MATLAB: Sum of several matrixes

summasion of matrixes

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

Best 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.