By element average of multiple matrices
조회 수: 65 (최근 30일)
이전 댓글 표시
Hi,
I have 40 matrices of 781x981 in dimension. I need to calculate the by element mean of all these matrices in a new, 41st matrix.
I.e., the mean of element 1,1 of all 40 matrices is returned as element 1,1 in the new, 41st matrix. The same is then done for all elements.
In additionto the mean, I need to do the same for the standard deviation also.
Is there a function in Matlab that can do this? Or do I have to write some kind of a loop??
Thanks, Bryan
댓글 수: 0
채택된 답변
Wayne King
2012년 4월 3일
Yes, concatenate the matrices into a 3-D array and use mean(x,3)
x = randn(4,4,10);
mean(x,3)
and
std(x,[],3)
To concatenate, you can use cat()
x = randn(4,4);
y = randn(4,4);
z = cat(3,x,y);
mean(z,3)
std(z,[],3)
추가 답변 (1개)
Sean de Wolski
2012년 4월 3일
Stack your 40 matrices into one three-dimensional matrix, then take the mean along the 3rd dimension
meanMatrix = mean(stackedMatrix,3); %doc mean for more info.
To stack your matrices look at these two FAQs:
댓글 수: 4
Belinda Finlay
2020년 2월 17일
I am very new to MATLAB and am attempting a do a similar calacuation; however, I am having trouble stacking my matrices (as suggested by Sean), is someone able to guide me.
Thanks
Belinda
Yash Ajay Garje
2022년 4월 18일
Hi Belinda,
I do it like this:
A(:,:,1) = ['you could enter your 1st matrix here']
A(:,:,2) = ['you could enter your 2nd matrix here']
A(:,:,3) = ['you could enter your 3rd matrix here']
....
A(:,:,n) = ['you could enter your nth matrix here']
As a result, you would have created a matrix 'A' with the third dimension as 'n'. You could also loop this if required. Hope this helps.
Best,
Yash
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!