How to concatenate matrices to calculate mean

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 comentarios

KSSV
KSSV el 20 de Oct. de 2020
Read about mean, you can specify the dimension along which you want mean.
Stephen23
Stephen23 el 20 de Oct. de 2020
Editada: Stephen23 el 20 de Oct. de 2020
"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.

Iniciar sesión para comentar.

Respuestas (2)

KSSV
KSSV el 20 de Oct. de 2020
Let A be your matrix.....use
iwant = mean(A,3) ;

8 comentarios

Joydeb Saha
Joydeb Saha el 20 de Oct. de 2020
Can you please help making the loop? Also if I use mean(A,3), shall it work for a 3 dimentional matrix ? as the matrix having the dimention 3x721x1440 and I want the same dimentional matrix as the mean matrix.
KSSV
KSSV el 20 de Oct. de 2020
Yes mean(A,3) calculates mean along 3 dimension.
Joydeb Saha
Joydeb Saha el 20 de Oct. de 2020
Can you please help making the loop?
C = {};
vals = 1979:1982;
for i = 1:numel(vals)
[~, C{i}] = evalc(sprintf('DJF%d_125', vals(i)));
end
A = cat(3,C{:});
M = mean(A,3);
i am using this loop, but the M comes as two dimentional form.
KSSV
KSSV el 20 de Oct. de 2020
Where you ahve all those matrices?
Joydeb Saha
Joydeb Saha el 20 de Oct. de 2020
workspace
Joydeb Saha
Joydeb Saha el 20 de Oct. de 2020
I think I need to write M = mean(A,3);
Joydeb Saha
Joydeb Saha el 20 de Oct. de 2020
I think I need to write M = mean(A,1);

Iniciar sesión para comentar.

Jan
Jan el 20 de Oct. de 2020
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 comentarios

Joydeb Saha
Joydeb Saha el 20 de Oct. de 2020
I am getting this error :
Error using cat
Names of fields in structure arrays being concatenated do not match. Concatenation of structure arrays requires that these arrays have the same set of fields.
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 el 20 de Oct. de 2020
@Jaydeb Saha: I do not know the contents of these files. Maybe you want to import a specific variable only?

Iniciar sesión para comentar.

Categorías

Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.

Preguntada:

el 20 de Oct. de 2020

Comentada:

Jan
el 20 de Oct. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by