Filter löschen
Filter löschen

how to sum multiple matrics inside a cell array?

7 Ansichten (letzte 30 Tage)
Matlab_Student
Matlab_Student am 11 Feb. 2018
for example:
A = cell(1,3);
A{1} = [1];
A{2} = [1];
A{3} = [1];
What's a quick command to sum these cell elements to return [3]? plus() only works for size of two.
  1 Kommentar
Stephen23
Stephen23 am 11 Feb. 2018
Why store numeric scalars in a cell array anyway? The efficient solution to this is to convert to a numeric vector and then use the inbuilt sum, so why not skip the middle steps of using the cell array and simply store the data in a numeric array?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

James Tursa
James Tursa am 11 Feb. 2018
result = sum([A{:}]);
  2 Kommentare
Stephen23
Stephen23 am 11 Feb. 2018
+1 neat and efficient
Mohammod Minhajur Rahman
Mohammod Minhajur Rahman am 17 Nov. 2018
Bearbeitet: Mohammod Minhajur Rahman am 17 Nov. 2018
Could you please suggest how it might work for matrices? I mean, if A{1} = magic(150); A{2} = magic(150); A{3} = magic(150); and the result should be a matrix of 150x150. Also, magic(150) is a sparse matrix?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by