how to sum multiple matrics inside a cell array?

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

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?

댓글을 달려면 로그인하십시오.

 채택된 답변

James Tursa
James Tursa 2018년 2월 11일
result = sum([A{:}]);

댓글 수: 2

+1 neat and efficient
Mohammod Minhajur Rahman
Mohammod Minhajur Rahman 2018년 11월 17일
편집: Mohammod Minhajur Rahman 2018년 11월 17일
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?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by