how to calculate number of zeroes in an image array?

2 次查看(过去 30 天)
suppose i have an image array for example
1 2 3 1 2 1 1
0 1 2 5 8 7 9
4 7 8 9 2 3 0
0 0 0 1 2 0 0
the above array has 7 zeroes so how to calculate it using a matlab code

采纳的回答

Image Analyst
Image Analyst 2016-1-10
Here are two ways:
m=[...
1 2 3 1 2 1 1
0 1 2 5 8 7 9
4 7 8 9 2 3 0
0 0 0 1 2 0 0]
numZeros = numel(m) - nnz(m)
numZeros = sum(sum(m==0))
  5 个评论
Adnan Saify
Adnan Saify 2016-1-10
i need one more help ,can we determine number of'0' as well as '1' in an array
Image Analyst
Image Analyst 2016-1-10
I already showed you how to do that - find 0's. For example:
numZeros = sum(sum(m==0))
or in general, to find any integer:
countOfThisNumber = sum(sum(yourArray == yourInteger))
so, to count the 1's, you'd do this:
countOf1s = sum(sum(yourArray == 1))
Of course you can use whatever name for the variables you want. You don't have to use the names I used but I do encourage you to use descriptive names, rather than just single letters, so that your code is maintainable/understandable by others (and you) later, and not just an incomprehensible alphabet soup like we see far too often.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by