Calculating a mean matrix from a large number of matrices

조회 수: 3 (최근 30일)
Aaron Smith
Aaron Smith 2017년 3월 13일
댓글: Aaron Smith 2017년 3월 15일
I have been trying to calculate an average matrix from 50 matrices. Using squeeze and cat, i have calculated the mean to two matrices. My question was; how is it possible to get the mean of 50 matrices. I have a code to open the files from a folder and reformat and bin them.
myFolder = uigetdir('C:\Users\c13459232\Documents\MATLAB');
if ~isdir(myFolder)
errorMessage = sprintf('Error: the following folder does not exist: \n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
outFolder = fullfile(myFolder, 'output');
mkdir(outFolder);
filePattern = fullfile(myFolder, '*.asc');
Files = dir(filePattern);
for k = 1 : length(Files)
baseFileName = Files(k).name;
FileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', FileName);
fid = fopen(FileName);
Cell = textscan( fid, '%d', 'delimiter', ';')
fclose(fid);
Data = cell2mat(Cell);
N = 1024;
skip = 2;
Finish0 = reshape(Data, N, [])';
Finish1 = Finish0(1:skip:end, 1:skip:end);
newFileName = fullfile(outFolder, ['finish_' sprintf('%03d', k) '.txt']);
dlmwrite(newFileName, Finish0, ';');
eval(['finish_' sprintf('%03d', k) ' = Finish1;']);
disp([':: Now writing ' newFileName]);
end
Where could i insert squeeze(cat()) to average all of these matrices. The example I found to get the mean of two matrices is:
c2 = squeeze(mean(cat(3,finish_002,finish_004), 3));
Would you suggest a variation on this code or is there a better way to get an average matrix?
  댓글 수: 9
Stephen23
Stephen23 2017년 3월 14일
@Aaron Smith: using indexing is going to be much easier to work with.
If you want to know why, read that link I gave you.
Guillaume
Guillaume 2017년 3월 14일
편집: Guillaume 2017년 3월 14일
"You see, when each full matrix is saved as a single cell in a cell array, I'm not sure if this will give me the freedom i need to plot each one individually and to bin each one individually"
It's the exact opposite. Having all the matrices as cells of a cell array gives you the freedom to treat each them individually any way you want. Use a fixed index in your cell array, and the freedom to operate on all them at once. Having individually named matrices you sacrifice the latter and don't gain anything in return.
As a rule, if you're numbering variables you're doing something wrong. These numbered variables are obviously related so they should be stored together in a container of some sort (matrix, cell array, containers.map, whatever).
This would make your question dead easy to solve:
%finish: cell array of matrices
meanofallmatrices = mean(cat(3, finish{:}), 3); %no need for squeeze
And since all the matrices are obviously the same size, you could store them directly as a 3D array instead of cell array (with again no loss of freedom)
Stephen is right, eval is completely the wrong approach. Here be dragons!

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

답변 (1개)

Thorsten
Thorsten 2017년 3월 13일
편집: Thorsten 2017년 3월 14일
In the for-loop, you can just use
if k == 1,
M = 1/length(Files)*Data;
else
M = M + 1/length(Files)*Data;
end
Then after the loop, M is the mean of the matrices.
  댓글 수: 10
Guillaume
Guillaume 2017년 3월 15일
편집: Guillaume 2017년 3월 15일
You've got a closing bracket in the wrong location, the second 3 is an argument to mean, not cat.
mean(cat(3, finishCell{:}), 3);
%bracket goes here ------^
That second 3 tells mean to average across pages instead of the default across rows as you saw.
Aaron Smith
Aaron Smith 2017년 3월 15일
Ah, I see.
Thanks a million Guillaume

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

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by