假设我有一个数组:
temp<-array(0, dim=c(100,10,4))
我可以合并矩阵1&amp; 2.使用cbind将数组从数组转换为单个矩阵:
temp.merge<-cbind(temp[,,1],temp[,,2])
有没有办法将所有n个矩阵(在本例中为4)合并为一个矩阵而不必像上面那样写出每个矩阵的位置?
答案 0 :(得分:5)
如果您已在内存中设置了阵列,则只需重置尺寸即可。
dim(temp) <- c(100, 40)
答案 1 :(得分:0)
如果@ Neal的回答有效,请明确使用它。
这也有效:
# generates 100 X 40 array
do.call("cbind",lapply(1:4,function(x){return(temp[,,x])}))
你会想:
do.call("cbind",list(temp[,,1:4])) ## generates 4000 X 1 array
会起作用,但它不会......
答案 2 :(得分:0)
此外:
as.matrix(as.data.frame(temp))
示例:
> temp <- array(1:8, dim=c(2,2,2))
> temp
#, , 1
#
# [,1] [,2]
#[1,] 1 3
#[2,] 2 4
#
#, , 2
#
# [,1] [,2]
#[1,] 5 7
#[2,] 6 8
as.matrix(as.data.frame(temp))
# V1 V2 V3 V4
#[1,] 1 3 5 7
#[2,] 2 4 6 8