- you use nested loops, but never use the outer loop variable i anywhere useful, so it just repeats exactly the same thing 24 times.
- the loop variable i remains totally unused here: sprintf('ii_Sub*.mat',i). There is nothing in that formatting string that uses any input variable.
- The call mean(a,24) makes no sense: a is a cell array, so taking mean of it is not defined. Even if it did work your array have 24 dimensions, so telling MATLAB to sum over the 24th dimension (which will be singular) serves no purpose whatsoever.
How can I get an average of different matrices stored as separate .mat files?
2 ビュー (過去 30 日間)
古いコメントを表示
Native
2018 年 11 月 13 日
コメント済み: João Vítor Batista Pires Santos
2021 年 7 月 20 日
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
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
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 Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!