MATLAB Answers

0

Count the index of the array

Asked by Mekala balaji on 20 Dec 2014
Latest activity Commented on by madhan ravi
on 3 Dec 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

  0 Comments

Sign in to comment.

3 Answers

Answer by Azzi Abdelmalek
on 20 Dec 2014
 Accepted Answer

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 Comments

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.
Great Thank you sir

Sign in to comment.


Answer by Shoaibur Rahman on 20 Dec 2014

A=[0 0 0 0 0 0 0 0 0 10 0 0 0 0 15 0 17];
sum(A>0)

  0 Comments

Sign in to comment.


Answer by Javaid Iqbal on 3 Dec 2018

n=numel(A);

  1 Comment

madhan ravi
on 3 Dec 2018
no this would give you total number of elements in the array

Sign in to comment.