Count the index of the array

5 次查看(过去 30 天)
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

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2014-12-20
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 个评论
Shoaibur Rahman
Shoaibur Rahman 2014-12-20
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.

请先登录,再进行评论。

更多回答(2 个)

Shoaibur Rahman
Shoaibur Rahman 2014-12-20
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 2018-12-3
n=numel(A);
  1 个评论
madhan ravi
madhan ravi 2018-12-3
no this would give you total number of elements in the array

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by