Summing elements of an array

1 vue (au cours des 30 derniers jours)
Gian Carpinelli
Gian Carpinelli le 13 Avr 2018
Commenté : Stephen23 le 13 Avr 2018
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 commentaire
Stephen23
Stephen23 le 13 Avr 2018
@Gian Carpinelli: why are you storing numeric data in a cell array? It would be simpler to store it in a numeric array.

Connectez-vous pour commenter.

Réponses (2)

KL
KL le 13 Avr 2018
Modifié(e) : KL le 13 Avr 2018
sum([P{:}])
But why would you store it in cell array? If they are all numeric, better use a numeric array.

M
M le 13 Avr 2018
You can use either
sum(cell2mat(P))
or
sum([P{:}])

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by