作者:youxiang574传奇_257 | 来源:互联网 | 2023-05-18 14:54
Iamwonderinghowtouseapplyonamultidimensionalarray.Ihavesomethinglikethefollowing:我想
I am wondering how to use apply on a multidimensional array. I have something like the following:
我想知道如何在多维数组上使用apply。我有类似以下内容:
A <- array(0, c(2, 2, 5))
for(i in 1:5) {
A[, , i] <- matrix(rnorm(4), 2, 2)
}
I would like to take the average of those slices to get a single 2 by 2 matrix. Any way I come up with is pretty kludgy.
我想取这些切片的平均值来得到一个2乘2的矩阵。我提出的任何方式都非常糟糕。
I was hoping to be able to use apply, like I would if I wanted the average say of the columns of a matrix:
我希望能够使用apply,就像我想要矩阵的列的平均说法一样:
B <- matrix(rnorm(10), 5, 2)
B.mean <- apply(B, 2, mean)
But this doesn't seem to work the way I think it might with 3D arrays:
但这似乎与我认为3D阵列的方式不同:
A.mean <- apply(A, 3, mean)
I appreciate your suggestions.
我感谢您的建议。
1 个解决方案