Count the index of the array

5 vues (au cours des 30 derniers jours)
Mekala balaji
Mekala balaji le 20 Déc 2014
Commenté : madhan ravi le 3 Déc 2018
Hi, I have a array like below: [0 0 0 0 0 0 0 0 0 10 0 0 0 0 15 0 17]
I want count how many numbers >0 in the array (like in this case count is 3 (because 10,15,&17). Can some please help me how to count this in matlab. Thanks in advance

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 20 Déc 2014
If your array doesn't contain negative numbers
a=[0 0 0 0 0 0 0 0 0 10 0 0 0 0 15 0 17]
out=nnz(a)
  2 commentaires
Shoaibur Rahman
Shoaibur Rahman le 20 Déc 2014
nnz will count the number of all non-zero elements in the array, including negatives. So, this function would be a better choice if you want to do so.
Mekala balaji
Mekala balaji le 20 Déc 2014
Great Thank you sir

Connectez-vous pour commenter.

Plus de réponses (2)

Shoaibur Rahman
Shoaibur Rahman le 20 Déc 2014
A=[0 0 0 0 0 0 0 0 0 10 0 0 0 0 15 0 17];
sum(A>0)

Javaid Iqbal
Javaid Iqbal le 3 Déc 2018
n=numel(A);
  1 commentaire
madhan ravi
madhan ravi le 3 Déc 2018
no this would give you total number of elements in the array

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by