0

我有几个矩阵,我想将它们加入一个数组,如下所示:

> mat1
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

> mat2
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

> mat3
     [,1] [,2] [,3]
[1,]   13   15   17
[2,]   14   16   18

我试过这个:

dime=dim(mat1)
Array=array(mat1, mat2,mat3,dim(dime))

出现以下错误:

Error in array(mat1, mat2, mat3, dim(dime)) : 
  unused argument(s) (dim(dime))

我可能做错了什么?

4

1 回答 1

2

更新:

从评论看来,所需要的只是cbind三个矩阵:

> cbind(mat1, mat2, mat3)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]    1    3    5    1    3    5    1    3    5
[2,]    2    4    6    2    4    6    2    4    6

我想如果你有很多这些,将它们安排在一个列表中然后一起使用它们是有意义do.callcbind

mlist <- list(mat1, mat2, mat3) ## simulate matrices stored as a list

## cbind them via a `do.call` call
do.call(cbind, mlist)

产生

> do.call(cbind, mlist)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]    1    3    5    1    3    5    1    3    5
[2,]    2    4    6    2    4    6    2    4    6

原来的

您希望如何在数组中排列矩阵并不是很清楚。如果您的意思是像一堆纸一样堆叠矩阵,每页纸都是一个矩阵,那么我们可以简单地将矩阵连接成一个向量,c然后array用适当的dim参数将其传递给。例如

> mat1 <- mat2 <- mat3 <- matrix(1:6, ncol = 3)
> array(c(mat1, mat2, mat3), dim = c(2,3,3))
, , 1

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

, , 2

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

, , 3

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
于 2013-04-03T05:38:31.510 回答