Effacer les filtres
Effacer les filtres

Summation of values > 1 in a cell array

1 vue (au cours des 30 derniers jours)
RDG
RDG le 12 Avr 2013
Suppose I've a cell array;
cnt{1}=[3;1;3;2;1;2]
I would like to sum up the values which are > 1 ie the answer should return 10 (3+3+2+2)
I've tried using 'find' but the answer only returns the indices of the values > 1 and not the values.
ind=(find(cnt{1}>1))
How can I go about this? Please give me some hint.

Réponse acceptée

Ilham Hardy
Ilham Hardy le 12 Avr 2013
sumVal = sum(cnt{1})-length(find(cnt{1}==1))
  3 commentaires
Ilham Hardy
Ilham Hardy le 12 Avr 2013
Yes, i agree. This way will be faster.
However i don't fully get the differences between example#1 and example#2. Care to explain?
Matt J
Matt J le 12 Avr 2013
The 2nd method would be needed if the threshold were something other than 1.

Connectez-vous pour commenter.

Plus de réponses (1)

Matt J
Matt J le 12 Avr 2013
Modifié(e) : Matt J le 12 Avr 2013
ind=(cnt{1}>1);
sum(cnt{1}(ind)),

Catégories

En savoir plus sur MATLAB 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