矩阵列表的均值 [英] Mean of list of matrices by element
本文介绍了矩阵列表的均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个矩阵列表:
.list <- list(matrix(1:25, ncol = 5), matrix(11:35, ncol = 5))
我想使用Reduce
方法在列表中查找矩阵的逐元素方法.
I would like to use the Reduce
method to find the element-by-element means of the matrices in the list.
换句话说,我正在寻找以下结果:
In other words, I am looking for the following result:
res = matrix(6:30, ncol = 5)
我尝试了以下操作:
res = Reduce(mean, .list)
但是我得到一个错误:
Error in mean.default(init, x[[i]]) :
'trim' must be numeric of length one
请注意,矩阵的元素可以是NA
.
任何帮助,将不胜感激!谢谢!!
Note that an element of a matrix can be NA
.
Any help would be appreciated! Thank you!!
推荐答案
我刚刚意识到可以通过以下方式(使用Reduce
函数)实现此目的:
I just realized that this could be achieved the following way (using the Reduce
function):
tmp = Reduce('+', .list)
result = tmp/length(.list)
这篇关于矩阵列表的均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文