Azzera filtri
Azzera filtri

how to calculate number of zeroes in an image array?

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Image Analyst
Image Analyst il 10 Gen 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 Commenti
Adnan Saify
Adnan Saify il 10 Gen 2016
i need one more help ,can we determine number of'0' as well as '1' in an array
Image Analyst
Image Analyst il 10 Gen 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.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by