简体   繁体   English

R中3D数组列表的平均值

[英]Mean of a list of 3D arrays in R

How can I calculate the means of arrays stored in a list in R? 如何计算R中列表中存储的数组的均值? The result should be an array of the same size containing the means... For example: 结果应该是包含均值的相同大小的数组。例如:

a1 <- array(runif(100), dim=c(4, 5, 5))
a2 <- array(runif(100), dim=c(4, 5, 5))
a3 <- array(runif(100), dim=c(4, 5, 5))
a4 <- array(runif(100), dim=c(4, 5, 5))
a5 <- array(runif(100), dim=c(4, 5, 5))

l <- list(a1, a2, a3, a4, a5)

[...]

should result in an array of dimension 4, 5, 5 containing the means. 应该导致包含该平均值的4、5、5维数组。

I can do it for a list of matrices with: 我可以使用以下列表来处理矩阵:

apply(simplify2array(myList), 1:2, mean)

but it doesn't work for my purpose... 但这对我的目的不起作用...

Thanks for any clue! 感谢您提供任何线索!

也许我们可以使用Reduce

Reduce(`+`, l)/length(l)

You could first transform you list of arrays into a 4D array and then use apply : 您可以先将数组列表转换为4D数组,然后使用apply

a <- array(unlist(l), dim = c(dim(l[[1]]), length(l)))
apply(a, 1:3, mean)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM