【问题标题】:Rotate a Matrix in R by 90 degrees clockwise将 R 中的矩阵顺时针旋转 90 度
【发布时间】:2013-05-05 23:04:39
【问题描述】:

我在 R 中有一个这样的矩阵:

|1|2|3|
|1|2|3|
|1|2|3|

有没有一种简单的方法可以将整个矩阵顺时针旋转 90 度以获得这些结果?

|1|1|1|
|2|2|2|
|3|3|3|

然后再旋转 90 度:

|3|2|1|
|3|2|1|
|3|2|1|

?

【问题讨论】:

  • 这叫做转置矩阵。试试函数t
  • 是的,但t 可以 360 度旋转吗?还是仅向右 90 度?

标签: r matrix transpose


【解决方案1】:
m <- matrix(rep(1:3,each=3),3)

     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    1    2    3
[3,]    1    2    3

t(m[nrow(m):1,])

     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    2    2    2
[3,]    3    3    3

m[nrow(m):1,ncol(m):1]

     [,1] [,2] [,3]
[1,]    3    2    1
[2,]    3    2    1
[3,]    3    2    1

t(m)[ncol(m):1,]

     [,1] [,2] [,3]
[1,]    3    3    3
[2,]    2    2    2
[3,]    1    1    1

【讨论】:

    【解决方案2】:

    t 不旋转条目,它沿对角线翻转:

    x <- matrix(1:9, 3)
    x
    ##      [,1] [,2] [,3]
    ## [1,]    1    4    7
    ## [2,]    2    5    8
    ## [3,]    3    6    9
    
    t(x)
    ##      [,1] [,2] [,3]
    ## [1,]    1    2    3
    ## [2,]    4    5    6
    ## [3,]    7    8    9
    

    R矩阵顺时针旋转90度:

    您还需要在转置之前反转列:

    rotate <- function(x) t(apply(x, 2, rev))
    rotate(x)
    ##      [,1] [,2] [,3]
    ## [1,]    3    2    1
    ## [2,]    6    5    4
    ## [3,]    9    8    7
    
    rotate(rotate(x))
    ##      [,1] [,2] [,3]
    ## [1,]    9    6    3
    ## [2,]    8    5    2
    ## [3,]    7    4    1
    
    rotate(rotate(rotate(x)))
    ##      [,1] [,2] [,3]
    ## [1,]    7    8    9
    ## [2,]    4    5    6
    ## [3,]    1    2    3
    
    rotate(rotate(rotate(rotate(x))))
    ##      [,1] [,2] [,3]
    ## [1,]    1    4    7
    ## [2,]    2    5    8
    ## [3,]    3    6    9
    

    R矩阵逆时针旋转90度:

    在反向之前进行转置与逆时针旋转相同:

    foo = matrix(1:9, 3)
    foo
    ## [,1] [,2] [,3]
    ## [1,]    1    4    7
    ## [2,]    2    5    8
    ## [3,]    3    6    9
    
    foo <- apply(t(foo),2,rev)
    foo
    
    ## [,1] [,2] [,3]
    ## [1,]    7    8    9
    ## [2,]    4    5    6
    ## [3,]    1    2    3
    

    【讨论】:

    • 漂亮且非常直观。谢谢!
    • apply 可能不是最优的;来自 R-help 档案:rotate = function(mat) t(mat[nrow(mat):1,,drop=FALSE])
    • 除了2次重复操作,还有逆时针旋转的论坛吗?对于大型矩阵,这是一个相当密集的过程。
    • @geotheory 简单颠倒操作顺序:apply(t(x), 2, rev)
    【解决方案3】:

    将矩阵旋转 180° 的简单方法是:

    m <- matrix(1:8,ncol=4)
    #      [,1] [,2] [,3] [,4]
    # [1,]    1    3    5    7
    # [2,]    2    4    6    8
    
    
    rot <- function(x) "[<-"(x, , rev(x))
    
    rot(m)
    #      [,1] [,2] [,3] [,4]
    # [1,]    8    6    4    2
    # [2,]    7    5    3    1
    
    rot(rot(m))
    #      [,1] [,2] [,3] [,4]
    # [1,]    1    3    5    7
    # [2,]    2    4    6    8
    

    【讨论】:

    • 这个函数的一个很好的特性是,如果你使用Matrix 类,它可以保持稀疏性。但是,您的代码在 R 3.2.1 中不起作用(不再?),它抱怨缺少 value 参数。相反,这种变化有效:rotate &lt;- function(x) {x[] &lt;- rev(x); x}.
    • 我应该提一下,尽管这保留了稀疏性,但它确实必须临时实例化一个非稀疏向量,其大小是矩阵维度的乘积。
    • @KenWilliams 我无法重现该问题。它仍然可以在我的 R 3.2.1 机器上运行。
    • 哦 - 它看起来只有在 mMatrix 对象时才会失败。当m 是香草matrix 时,它确实工作得很好。
    • @PoGibas 函数[&lt;- 用于索引替换。在代码x[i] &lt;- y 中,对象x 将被更改。如果你改用"[&lt;-"(x, i, y),修改后的对象会被返回,x不会改变。
    【解决方案4】:

    将矩阵旋转 90 度和 -90 度的 R 方法

    #first reverse, then transpose, it's the same as rotate 90 degrees
    rotate_clockwise         <- function(x) { t(     apply(x, 2, rev))}
    #first transpose, then reverse, it's the same as rotate -90 degrees:
    rotate_counter_clockwise <- function(x) { apply(     t(x),2, rev)}
    
    #or if you want a library to help make things easier to read:
    #install.packages("pracma")
    library(pracma)
    rotate_one_eighty <- function(x) { rot90(x, 2) }
    rotate_two_seventy <- function(x) { rot90(x, -1) }
    
    foo = matrix(1:9, 3)
    foo
    
    foo = rotate_clockwise(foo)
    foo
    
    foo = rotate_counter_clockwise(foo)
    foo
    
    foo = rotate_one_eighty(foo)
    foo
    

    打印:

         [,1] [,2] [,3]
    [1,]    1    4    7          #original matrix
    [2,]    2    5    8
    [3,]    3    6    9
         [,1] [,2] [,3]
    [1,]    3    2    1
    [2,]    6    5    4          #rotated 90 degrees
    [3,]    9    8    7
         [,1] [,2] [,3]
    [1,]    1    4    7
    [2,]    2    5    8          #rotated -90 degrees
    [3,]    3    6    9
         [,1] [,2] [,3]
    [1,]    9    6    3
    [2,]    8    5    2          #rotated 180 degrees
    [3,]    7    4    1
    

    请注意,顺时针旋转矩阵,然后逆时针将数字返回到它们的原始位置,然后旋转 180 就像旋转 90 两次一样。

    【讨论】:

      【解决方案5】:

      或组合成一个函数(基于 Eric Leschinski):

      rotate  <- function(x, clockwise=T) {
        if (clockwise) { t( apply(x, 2, rev))
        } else {apply( t(x),2, rev)} 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-04
        • 1970-01-01
        • 1970-01-01
        • 2019-11-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多