How to concatenate matrices to calculate mean

조회 수: 4 (최근 30일)
Joydeb Saha
Joydeb Saha 2020년 10월 20일
댓글: Jan 2020년 10월 20일
I have 40 matrices DJF1979_1980_125 to DJF2019_2020_125. Each matrix is 3x721x1440. I want a mean of all the matrices, that will be a single matrix. Please help.
  댓글 수: 2
KSSV
KSSV 2020년 10월 20일
Read about mean, you can specify the dimension along which you want mean.
Stephen23
Stephen23 2020년 10월 20일
편집: Stephen23 2020년 10월 20일
"I have 40 matrices DJF1979_1980_125 to DJF2019_2020_125."
Accessing lots of numbered variables forces you into writing slow, ineffiicient, complex, buggy code:
The much better approach is to load data into an output variable
S = load(..)
and access its fields. Or use indexing with any type of array.

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

답변 (2개)

KSSV
KSSV 2020년 10월 20일
Let A be your matrix.....use
iwant = mean(A,3) ;
  댓글 수: 8
Joydeb Saha
Joydeb Saha 2020년 10월 20일
I think I need to write M = mean(A,3);
Joydeb Saha
Joydeb Saha 2020년 10월 20일
I think I need to write M = mean(A,1);

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


Jan
Jan 2020년 10월 20일
As Steven has mentioned already, this is the main problem:
"I have 40 matrices DJF1979_1980_125 to DJF2019_2020_125"
Do not use strange names of variables to store importand data, bevause this impedes the processing. If you load these variables from files, store them in an array instead:
List = dir('*.mat');
Data = cell(size(List));
for k = 1:numel(List)
Data{k} = load(fullfile(List(k).folder, List(k).name));
end
Then you can apply the mean() function either is another loop, or by cellfun, or concatenate the varibales in a 4th dimension at first:
AllData = cat(4, Data{:});
MeanData = mean(AllData, 3);
  댓글 수: 3
Stephen23
Stephen23 2020년 10월 20일
If every mat file contain exactly one variable then you can do this inside the loop:
fnm = fullfile(List(k).folder, List(k).name);
Data(k) = struct2cell(load(fnm));
Jan
Jan 2020년 10월 20일
@Jaydeb Saha: I do not know the contents of these files. Maybe you want to import a specific variable only?

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

카테고리

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