Sum of several matrixes

Hello, I have 500 matrixes with the same dimension. I want to add these matrixes. I would be grateful if you help me.

 Accepted Answer

Here is one option:
M1 = randi(10, 3, 4); % Create (3x4) Matrices
M2 = randi(10, 3, 4);
M3 = randi(10, 3, 4);
M4 = randi(10, 3, 4);
M5 = randi(10, 3, 4);
M = [];
for k1 = 1:5; % Concatenate Them
M = cat(3, M, eval(sprintf('M%d', k1)));
end
Msum = sum(M, 3); % Sum Them
If your matrices all have similar names, this will work. (If they don’t I can only give you sympathy.) It first concatenates them along the third dimension in the loop (a legitimate use of ‘eval’), then adds them along the third dimension.

2 Comments

RSM
RSM on 16 Jan 2015
Thank you very much, I will try
My pleasure!

Sign in to comment.

More Answers (0)

Categories

Asked:

RSM
on 15 Jan 2015

Commented:

on 16 Jan 2015

Community Treasure Hunt

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

Start Hunting!