[英]How to obtain the mean-matrix from a list of matrices in R
我有一个存储在名为all_permutations的变量中的R中100个50 * 50矩阵的列表。
> str(all_permutations)
List of 100
$ : num [1:50, 1:50] 0 0.00972 0.34989 0 0.0019 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:50] "G1" "G2" "G3" "G4" ...
.. ..$ : chr [1:50] "G1" "G2" "G3" "G4" ...
$ : num [1:50, 1:50] 0 0.00972 0.34989 0 0.0019 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:50] "G1" "G2" "G3" "G4" ...
.. ..$ : chr [1:50] "G1" "G2" "G3" "G4" ...
是否有一种优雅的方法来获取所有这些矩阵的均值,而无需构造双for循环来获取所有100个矩阵的每个索引的平均值? 谢谢。
如果要获取每个位置元素的平均值,则需要对all_permutations
的元素all_permutations
,然后除以元素数。
如果要输入,您将执行以下操作:
(all_permutations[[1]] + all_permutations[[2]] + ... ) / length(all_permutations)
幸运的是, Reduce
函数可以为您节省很多输入(或更可能是for
循环):
Reduce("+", all_permutations) / length(all_permutations)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.