每行矢量化矩阵

时间:2013-12-16 08:57:05

标签: r vector matrix vectorization

我想对矩阵进行矢量化以获得“每行”的值。例如:

mat = matrix(1:4,ncol=2)
mat
     [,1] [,2]
[1,]    1    3
[2,]    2    4

c(mat) # or as.vector(mat). Both give the values "per columns"
[1] 1 2 3 4

我想得到这个:

[1] 1 3 2 4

2 个答案:

答案 0 :(得分:2)

首先转置矩阵,例如c(t(mat))

答案 1 :(得分:-2)

按行填充

matrix(1:4,ncol=2 , byrow=TRUE)  # matrix is filled by rows.

enter image description here

相关问题