簡體   English   中英

如何從R中的矩陣列表中獲得均值矩陣

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM