How can I average multiple matrices by element to create a new matrix of the same size?
Mostra commenti meno recenti
I have 31 matrices, each of size 72x144, and each representing a day of the month of January. I would like to have a 32nd matrix, also of size 72x144, which includes the average value for each element in the original 31 matrices. For example, if I have:
A=[1,2,3|2,3,4|3,4,5] B=[2,3,4|3,4,5|4,5,6] C=[3,4,5|4,5,6|5,6,7]
I want:
D=[2,3,4|3,4,5|4,5,6] (the average by element of the originals)
What is the easiest way for me to do this? I have found a few threads here which have answers I don't really understand, as I am relatively unfamiliar with writing codes, for MATLAB or otherwise. I need something easy to understand.
Thanks, Patrick
2 Commenti
Azzi Abdelmalek
il 2 Ott 2013
How those matrices are represented? A,B,C,D,E,...
Patrick
il 2 Ott 2013
Risposta accettata
Più risposte (3)
Azzi Abdelmalek
il 2 Ott 2013
M={A,B,C,D,E};
B=cat(3,M{:})
out=mean(B,3)
Matt J
il 2 Ott 2013
Read the matrices into the slices A(:,:,i) of a 3D array and do
Amean=mean(A,3);
Tope Oyelade
il 20 Apr 2021
Modificato: Image Analyst
il 20 Apr 2021
Just add a dot before the '/' as below and you are good to go!
meanMatrix = (matrix1 + matrix2 + matrix3 + .... etc...... + matrix30 + matrix31)./31
6 Commenti
Image Analyst
il 20 Apr 2021
Tope, the dot is not needed before division by a scalar, such as 31. It is only needed if you need to do an element-by-element division by an array.
Tope Oyelade
il 20 Apr 2021
Thanks @Image Analyst. I have just tried it out and the division works without the 'dot'...but the matrices being the same size adds up by just using the + sign and division by the number of matrices gave me the average of each elemment within the matrices.
Thanks again
Image Analyst
il 20 Apr 2021
Of course adding and dividing gives you the average - not sure why you said "but" as if something is different from what I said.
Sebastian Garzon
il 18 Lug 2022
I have a similar problem, but I want to disregard zero values from each cell when calculating the average. For instance, if i am summing 31 matrices two of the matrices have a zero value in cell 1X1 then the cell 1X1 from the resulting matrix would be the sum of the 29 remaining values divided by 29 rather than 31.
Bruno Luong
il 18 Lug 2022
Modificato: Bruno Luong
il 18 Lug 2022
@Sebastian Garzon Replace 0 with NaN the use 'omitnan' argumant
A(A==0) = NaN;
meanA = mean(A, 3, 'omitnan')
Or non destructive way:
meanA = sum(A,3) ./ sum(A~=0,3)
Image Analyst
il 18 Lug 2022
@Sebastian Garzon with Bruno's way you'd need to create A as a 3-D matrix:
A = cat(3, matrix1, matrix2, matrix3, .... etc...... , matrix30, matrix31);
A(A==0) = NaN;
meanA = mean(A, 3, 'omitnan')
Here's another way:
countMatrix = (matrix1 ~= 0 + matrix2 ~= 0 + matrix3 ~= 0 + .... etc...... + matrix30 ~= 0 + matrix31 ~= 0);
sumMatrix = (matrix1 + matrix2 + matrix3 + .... etc...... + matrix30 + matrix31);
averageMatrix = sumMatrix ./ countMatrix;
Categorie
Scopri di più su Operators and Elementary Operations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!