Summing elements of an array

조회 수: 2 (최근 30일)
Gian Carpinelli
Gian Carpinelli 2018년 4월 13일
댓글: Stephen23 2018년 4월 13일
Hi, so for example I have P such that: P =
3×1 cell array
[1]
[2]
[3].
The matrix P can be a n x 1 cell array, where n is any positive integer, and can only hold numbers. Ive tried to sum these numbers using sum(P) but keep getting the error ' Undefined function 'sum' for input arguments of type 'cell'. '. Any ideas on how I could do this? thank-you.
  댓글 수: 1
Stephen23
Stephen23 2018년 4월 13일
@Gian Carpinelli: why are you storing numeric data in a cell array? It would be simpler to store it in a numeric array.

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

답변 (2개)

KL
KL 2018년 4월 13일
편집: KL 2018년 4월 13일
sum([P{:}])
But why would you store it in cell array? If they are all numeric, better use a numeric array.

M
M 2018년 4월 13일
You can use either
sum(cell2mat(P))
or
sum([P{:}])

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by