首页 > 解决方案 > 如何从多维数组到维护组的一维

问题描述

我有一个 4 维数组:位置(3)x 物种(3)x 季节(6)x 深度(2)。像这个矩阵一样 12 次。

Season = 1, depth =  1
    [A] [B] [C]
[a] 12  52  55
[b] 13  14  235
[c] 13  76  355

我想将所有内容合并到一个大矩阵中,例如:

Season = 1, depth =  1
    [A] [B] [C]
[a11] 12  52  55
[b11] 13  14  235
[c11] 13  76  355
[a12] 12  52  55
[b12] 13  14  235
[c12] 13  76  355
[a21] 12  52  55
[b21] 13  14  235
[c21] 13  76  355
... 

等等。第一个数字表示一个额外的维度,第二个表示另一个维度。是否有意义?任何想法?

非常感谢!!:)

标签: rarraysmerge

解决方案


这将数组转置aperm,然后生成一个矩阵。

location = 3
species = 3
Season = 6
Depth = 2

set.seed(1)
myArr <- array(sample(1000, location * species * Season * Depth), dim = c(location, species, Season, Depth))
myArrPerm <- aperm(myArr, perm = c(1,3,4,2))

matrix(myArrPerm, ncol = species)

推荐阅读