Summing all the Matrices in a Cell Array?

14 Ansichten (letzte 30 Tage)
hithere
hithere am 5 Nov. 2014
Bearbeitet: Matt J am 5 Nov. 2014
Lets say i have a cell array with 3 7x2 matrices in it:
.
.
How do i sum them up so to get a single 7x2 matrix?
In this exmaple, i only have 3 matrices.
But i may have a cell array with maybe 100+ matrices.
How should i do it?
  1 Kommentar
Matt J
Matt J am 5 Nov. 2014
Bearbeitet: Matt J am 5 Nov. 2014
You probably should not be in this situation to begin with. Whatever code you're using to create the cell array, should probably be organizing the data as a 7x2xP numeric array instead. Most operations on an MxNxP array will be more efficient than with the MxN slices split across P cells.
An exception to this is when M*N is large and you're trying to avoid the copies that MATLAB makes when you do things like
a=A(:,:,i)
but the slices would have to be very very large to warrant that.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 5 Nov. 2014
Bearbeitet: Azzi Abdelmalek am 5 Nov. 2014
a={rand(7,2) rand(7,2) rand(7,2)} % This is an example
b=sum(cat(3,a{:}),3)

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by