How can I add many matrices?

1 view (last 30 days)
Adrian
Adrian on 22 Apr 2014
Commented: Adrian on 22 Apr 2014
For example, I have 1000 matrices, each of 500x120 in size: M(1), M(2), ..., M(1000)
I want to add in order to obtain a final matrix also of 500x120 in size: A = M(1) + M(2) + ... + M(1000) where A = [500x120]
I tried in many ways, but I couldn't do it. Could someone please give a hand? Thank you!
  2 Comments
Jan
Jan on 22 Apr 2014
What exactly is "M(1)"? Is this a cell?
Adrian
Adrian on 22 Apr 2014
M(1) is the first 500x120 matrix from all 1000 matrices. I just want to be able to add many matrices. Instead of M(1), M(2),...M(1000), you can consider: a, b,...,n where n = 1000
So I want: A = a + b + ... + n where n = 1000
Thanks again!

Sign in to comment.

Answers (1)

Jan
Jan on 22 Apr 2014
If the matrices are stored in a cell, convert it to a 3D array at first:
M{1} = rand(500, 120);
M{2} = rand(500, 120);
M{3} = rand(500, 120);
... etc
MM = cat(3, M{:});
Or if you have such a 3D array MM(500, 120, n) already:
Result = sum(MM, 3);

Categories

Find more on Multidimensional Arrays 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!