concatenation of few matrix with a loop

C{1} = [total1979_125_30];
C{2} = [total1980_125_30];
C{3} = [total1981_125_30];
C{4} = [total1982_125_30];
C{5} = [total1983_125_30];
...upto C{40} = [total2019_125_30];
just help me making the loop
So that I can use
A = cat(3,C{:});
M = mean(A,3);

2 Comments

The cause of your problem is that you put meta-data (e.g. dates) into variables names. Meta-data is data and belongs in variables, not in variable names. Once you have made that bad design decision, you force yourself into writing slow, complex, inefficient, obfuscated, buggy code to try and access those multiple data arrays:
So far you have not told us the most important information: how did you get those arrays into the workspace? Did you write them all out by hand? My guess is that you used load in a loop, in which case you can fix the problem at its cause by loading into an output variable, e.g.:
N = the number of files
C = cell(1,N);
for k = 1:N
F = the k-th filename
S = load(F);
C(k) = struct2cell(S);
end
Note how this does not require evil eval anywhere. Once you tell us how you actually get those multiple variables into the workspace, someone can help you to import/create the data in a better way.

Sign in to comment.

 Accepted Answer

Ameer Hamza
Ameer Hamza on 7 Oct 2020
Edited: Ameer Hamza on 7 Oct 2020
It was not a good idea to create a variable name like this: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. Always use a cell array. Since you already created these, you can use the following loop to create the cell array now.
C = {};
vals = 1979:2019;
for i = 1:numel(vals)
[~, C{i}] = evalc(sprintf('total%d_125_30', vals(i)));
end

12 Comments

how can I deduct a specific matrix from all other 40 matrices using a loop?
as like...
C1 = (total1979_125_30-M);
C2 = (total1980_125_30-M);
C3 = (total1981_125_30-M);
.
.
.
C40 = (total2019_125_30-M);
What dou mean by deduct?
You can use cellfun(). For example, if your matrix name is M and cell array is C
M % matrix to subtract
C % 1x40 cell array of matrices
C_new = cellfun(@(x) x-M, C, 'uni', 0);
"how can I deduct [subtract] a specific matrix from all other 40 matrices using a loop?"
If you stored all of the data in one 3D array, then with one simple subtraction.
Joydeb Saha comment posted as answer moved here
Ameer Hamza its showing the error : Undefined operator '-' for input arguments of type 'cell'.
the cell array C is in 1x41 cell form and M is in 12x1 double form
Can you attach the variables C and M as a .mat file?
Yes I have attached.
Fix this typo:
C_new = cellfun(@(x) x-M, C, 'uni', 0);
% ^ ^ must be the same !
Jaydeb, as Stephen mentioned, there was a typo. The corrected line works fine.
Or the alternate solution suggested by Stephen to use a 3D array
C = cat(3, C{:});
M_new = C - M;
M_new will also be a 3D array.
Yes, its working. Thank you very much for both your support.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!