How can I add many matrices?

2 visualizzazioni (ultimi 30 giorni)
Adrian
Adrian il 22 Apr 2014
Commentato: Adrian il 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 Commenti
Jan
Jan il 22 Apr 2014
What exactly is "M(1)"? Is this a cell?
Adrian
Adrian il 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!

Accedi per commentare.

Risposte (1)

Jan
Jan il 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);

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by