Convert a matrix to a 1 dimensional array

后端 未结 10 1513
忘了有多久
忘了有多久 2020-11-27 14:45

I have a matrix (32X48).

How can I convert the matrix into a single dimensional array?

10条回答
  •  被撕碎了的回忆
    2020-11-27 15:28

    Either read it in with 'scan', or just do as.vector() on the matrix. You might want to transpose the matrix first if you want it by rows or columns.

    > m=matrix(1:12,3,4)
    > m
         [,1] [,2] [,3] [,4]
    [1,]    1    4    7   10
    [2,]    2    5    8   11
    [3,]    3    6    9   12
    > as.vector(m)
     [1]  1  2  3  4  5  6  7  8  9 10 11 12
    > as.vector(t(m))
     [1]  1  4  7 10  2  5  8 11  3  6  9 12
    

提交回复
热议问题