Adding cell array entries

조회 수: 1 (최근 30일)
AA
AA 2020년 8월 23일
댓글: madhan ravi 2020년 8월 24일
Suppose you have a 162*24 cell array called CC and each cell array entry consist of a matrix.
How would you add CC{1,1}+CC{2,1}+CC {3,1}.....CC{162,1}
and then to the same for the remaining 23 columns.
I dont need the sum but all matrices within each cell entry have to be added.
Thanks

채택된 답변

Walter Roberson
Walter Roberson 2020년 8월 23일
nd = ndims(CC{1,1});
sol = sum(cat(ndims+1, CC{:,1}),ndims+1);
There are also approaches using the more obscure fold() operation.
  댓글 수: 4
AA
AA 2020년 8월 24일
I get an error Not enough input arguments and and also an unspecific error in the second line. Maybe the nd variable is missing in the second line?
madhan ravi
madhan ravi 2020년 8월 24일
nd = ndims(CC{1,1});
sol = sum(cat(nd+1, CC{:,1}),nd+1);

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

추가 답변 (1개)

Sara Boznik
Sara Boznik 2020년 8월 23일
for i=1:24
sol=sum((CC(:,i)))
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 8월 23일
No, not for a cell array. sum() does not apply to cell arrays.
Also, you are overwriting all of sol each iteration.

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

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by