将矩阵组合成R中的数组

时间:2013-03-05 00:00:59

标签: arrays r matrix

如果我创建了几个矩阵,我如何将它们组合成一个数组?我有8个矩阵,每个矩阵有200行和200列,我需要将它们组合成一个dim = 200,200,8的数组。所以我希望我的每个矩阵都是我阵列的一部分。

7 个答案:

答案 0 :(得分:28)

您可以使用abind包中的abind功能:

library(abind)
newarray <- abind( mat1, mat2, mat3, mat4, along=3 )

## or if mats are in a list (a good idea)

newarray <- abind( matlist, along=3 )

答案 1 :(得分:18)

这是两个例子。你可以轻松地将其扩展到八个

# create two matricies with however many rows and columns
x <- matrix( 1:9 , 3 , 3 )
y <- matrix( 10:18 , 3 , 3 )
# look at your starting data
x
y

# store both inside an array, with the same first two dimensions,
# but now with a third dimension equal to the number of matricies
# that you are combining
z <- array( c( x , y ) , dim = c( 3 , 3 , 2 ) )

# result
z

答案 2 :(得分:7)

这里的版本与abind类似,但不使用任何其他套餐。将所有内容收集到list中,然后使用sapply选项将simplify=添加到"array",之后不对列表的每个部分执行任何操作({{1}只返回对象,相当于identity):

function(x) x

如果要将新数组中每个原始矩阵的名称保留为标识符,请尝试:

sapply(list(x,y), identity, simplify="array")
# similarly to save a couple of keystrokes, the following is usually identical
sapply(list(x,y), I, simplify="array")

#, , 1
#
#     [,1] [,2] [,3]
#[1,]    1    4    7
#[2,]    2    5    8
#[3,]    3    6    9
#
#, , 2
#
#     [,1] [,2] [,3]
#[1,]   10   13   16
#[2,]   11   14   17
#[3,]   12   15   18

未来我还注意到这相当于sapply(mget(c("x","y")), identity, simplify="array")

simplify2array

答案 3 :(得分:2)

这取决于您是否要将它们组合为column-major或row-major。这类似于使用cbindrbind将向量组合到矩阵中。因为R以列主顺序存储矩阵,所以这是最容易实现的:

matrices <- list(
  matrix( 1:9 , 3 , 3 ),
  matrix( 10:18 , 3 , 3 )
);

#it is assumed all matrices in the list have equal dimensions
array1 <- array(
  data = do.call(cbind, matrices), 
  dim = c(dim(matrices[[1]]), length(matrices))
);

新维度(本例中为2)将成为第三维度。从打印方法的输出来看,这看起来很准确,因为它将打印分割为最后一个维度:

> print(array1)
, , 1

     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

, , 2

     [,1] [,2] [,3]
[1,]   10   13   16
[2,]   11   14   17
[3,]   12   15   18

但是,有时您可能需要将它们组合在第一个维度上,例如:

array2 <- array (
  data = do.call(rbind, lapply(matrices, as.vector)), 
  dim = c(length(matrices), dim(matrices[[1]]))
);

print(array2[1,,])

     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9 

例如,假设您要将这些矩阵分配给具有列的数据框;每行一个矩阵。然后第一个维度,a.k.a nrow必须在数组和数据框中匹配:

 mydf <- data.frame(foo = 1:2, row.names=c("first", "second"))
 mydf$bar <- array1
  Error in `$<-.data.frame`(`*tmp*`, "bar", value = 1:18) : 
    replacement has 3 rows, data has 2

 mydf$bar <- array2
 mydf$bar

答案 4 :(得分:0)

这个怎么样:

combmat <- array(dim=c(200,200,8), data=cbind(matrix1,matrix2,...,matrix8) )

答案 5 :(得分:0)

相关:How to stack multiple matrices in R

到目前为止,所有解决方案的问题在于,当此data.framedplyr的矩阵(不是data.table s - 工作正常)没有相同的行和列顺序时, bind会将值叠加在彼此不相关的值上。

如果您想检查并考虑每个维度中的名称,请查看narray

enter image description here

(免责声明:我写了包裹)

答案 6 :(得分:0)

library('abind')
abind(m1, m2, m3, along = 2.5)
abind(m1, m2, m3, along = 3)

m4 <- list(m1, m2, m3)
abind(m4, along = 3) 

       along        input = matrix + matrix                       output 
----------------------------------------------------------------------------
        0         split columns and row bind them                 array
        0.5       same as 0                                       array
        1         combine matrices into one matrix by rowwise     matrix
        1.5       split columns and column bind them              array
        2         combine matrices into one matrix by columnwise  matrix
        2.5       Form an array with matrices                     array
        3         Same as 2.5                                     array