如何计算多个矩阵的均值 [英] how to calculate mean of multiple matrices

查看:649
本文介绍了如何计算多个矩阵的均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2000个尺寸为27 * 27的协方差矩阵,我想获得所有2000个矩阵的平均协方差矩阵.我想要的结果是一个大小为27x27的矩阵,其中位置[1,1]是给定27个矩阵的位置[1,1]的平均值. 从其他帖子中可以看出,我应该创建一个数组并使用apply函数,但是它不起作用!

I have 2000 covariance matrices of size 27*27, I want to get the mean covariance matrix over all 2000 matrices. The result I want is one matrix of size 27x27 in which position [1,1] is the mean of position [1,1] of the given 27 matrices. I could see from other posts that I should make an array and use apply function, but it does not work!

我的代码:

a<-array(ml.1[c(1:2000)])
apply(a,c(1,2),mean)

我收到此错误消息: if(d2 == 0L){时错误:缺少值,需要TRUE/FALSE

I get this error message: Error in if (d2 == 0L) { : missing value where TRUE/FALSE needed

如果有人能帮助我解决这个问题,我将不胜感激.

I would appreciate if anyone can help me to solve this problem.

推荐答案

首先,@ eipi10是正确的,您的问题不可重现.但是这里的关键是如何设置阵列.

First, @eipi10 is right your're question is not reproducible. But the key here is in how you set up your array.

#Make some fake data 10 matrices 10x10
m <- lapply(1:10, function(x) matrix(rnorm(100), nrow = 10))
#bind the matrices together
a <- do.call(cbind, m)
#recast the matrix into three dimensions
dim(a) <- c(10,10,10) 
#now apply should work
apply(a, c(1,2), mean)

这篇关于如何计算多个矩阵的均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆