how to calculate number of zeroes in an image array?
1 view (last 30 days)
Show older comments
Adnan Saify
on 10 Jan 2016
Commented: Image Analyst
on 10 Jan 2016
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
0 Comments
Accepted Answer
Image Analyst
on 10 Jan 2016
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 Comments
Image Analyst
on 10 Jan 2016
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.
More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!