Rotate a Matrix in R

前端 未结 5 1148
情歌与酒
情歌与酒 2020-11-27 12:08

I have a matrix in R like this:

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

Is there an easy way to rotate the entire matrix by 90 degrees clockwise to get the

5条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 12:23

    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
    

提交回复
热议问题