필터 지우기
필터 지우기

How can I get an average of different matrices stored as separate .mat files?

조회 수: 2 (최근 30일)
I have 24 different matrices of dimension 68X68, stored in a folder. I need to create a new matrix that is an average of all those matrices. Using previous FAQ from this forum, I tried :
Num = 24
for i = 1:Num
files = dir(sprintf('ii_Sub*.mat',i))
for k = 1:length(files)
fprintf('Current file : %s\n', files(k).name)
a = load(files(k).name)
meanmatrix = mean(a,24)
end
end
This is creating a 1X1 struct only, while the mean command shows an error as well. How best can I go about it?
  댓글 수: 3
Stephen23
Stephen23 2018년 11월 13일
@Native: please do not close threads that have already have answers.
Native
Native 2018년 11월 13일
편집: Native 2018년 11월 13일
Sure, I was not aware of that. Closed it thinking that the purpose of the question was solved..
As for your earlier comment, thanks for pointing out the fallacies. I must admit that i have started to know the language of code only for a month now, so work in progress. Hope to learn a lot from this forum! :)

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

채택된 답변

Stephen23
Stephen23 2018년 11월 13일
편집: Stephen23 2018년 11월 13일
Assuming that each file contains exactly one variable (a matrix) and that all matrices are the same size and type:
S = dir('*.mat');
N = numel(S);
C = cell(1,N);
for k = 1:N
T = load(S(k).name)
C(k) = struct2cell(T);
end
M = cat(3,C{:})
mean(M,3)
  댓글 수: 9
Stephen23
Stephen23 2021년 7월 20일
@João Vítor Batista Pires Santos: you could try something like this:
for k = 1:N
C{k} = ncread(S(k).name,'PRMSL_L101'); % PRMSL_L101 = Pressure (17x17x4)
end
A = cat(4,C{:});
M = mean(A,4)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by