How can I average multiple matrices by element to create a new matrix of the same size?

조회 수: 173 (최근 30일)
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
Patrick
Patrick 2013년 10월 2일
Azzi, please see my comment in reply to Image Analyst's answer. I was mistakenly unclear about the file names, and that adds some value to the correct answer, I presume. Thanks for your help!

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2013년 10월 2일
meanMatrix = (matrix1 + matrix2 + matrix3 + .... etc...... + matrix 30 + matrix31)/31;
  댓글 수: 2
Patrick
Patrick 2013년 10월 2일
Awesome, and thank you so much for the quick reply! However, is there an easier way than that, even? I should have been more clear - the matrix .txt files are for the month of January 2004, and are written in the format "WP20040101.txt", "WP20040102.txt", etc.
And, on that topic - I am ultimately going to need to find an average matrix of the same size using data for the whole year (366 files, since 2004 was a leap year). After "WP20040131.txt", I have "WP20040201.txt". The answer you have provided would require me to type the file names for 366 separate matrices.
Since they are in chronological order, is there an easier way to write the code?
Thanks, Patrick
Image Analyst
Image Analyst 2013년 10월 2일
편집: Image Analyst 2013년 10월 2일
If you don't already have them in arrays, then you can use the code in the FAQ to read in the arrays from a bunch of files: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
myFolder = 'C:\Documents and Settings\yourUserName\My Documents\My Data Files';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.txt');
txtFiles = dir(filePattern);
for k = 1:length(txtFiles)
baseFileName = txtFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
thisArray = csvread(fullFileName); % Or however you read them in.
if k == 1
sumArray = thisArray;
else
sumArray = sumArray
end
end
meanArray = sumArray / length(txtFiles);

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 10월 2일
M={A,B,C,D,E};
B=cat(3,M{:})
out=mean(B,3)

Matt J
Matt J 2013년 10월 2일
Read the matrices into the slices A(:,:,i) of a 3D array and do
Amean=mean(A,3);

Tope Oyelade
Tope Oyelade 2021년 4월 20일
편집: Image Analyst 2021년 4월 20일
Just add a dot before the '/' as below and you are good to go!
meanMatrix = (matrix1 + matrix2 + matrix3 + .... etc...... + matrix30 + matrix31)./31
  댓글 수: 6
Bruno Luong
Bruno Luong 2022년 7월 18일
편집: Bruno Luong 2022년 7월 18일
@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
Image Analyst 2022년 7월 18일
@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;

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by