[英]How to obtain the mean-matrix from a list of matrices in R
I have a list of 100 50*50 matrices in R stored in a variable called all_permutations. 我有一个存储在名为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" ...
Is there an elegant way to obtain the mean of all these matrices without constructing double for-loops to get the average for each index across all 100 matrices? 是否有一种优雅的方法来获取所有这些矩阵的均值,而无需构造双for循环来获取所有100个矩阵的每个索引的平均值? Thank you.
谢谢。
If you want to get the averages of the elements in each position, you would want to sum up the elements of all_permutations
and then divide by the number of elements. 如果要获取每个位置元素的平均值,则需要对
all_permutations
的元素all_permutations
,然后除以元素数。
If you were typing this out, you would do something like: 如果要输入,您将执行以下操作:
(all_permutations[[1]] + all_permutations[[2]] + ... ) / length(all_permutations)
Luckily, the Reduce
function can save you a lot of typing (or, more likely, a for
loop): 幸运的是,
Reduce
函数可以为您节省很多输入(或更可能是for
循环):
Reduce("+", all_permutations) / length(all_permutations)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.