Counting zeros

1 Ansicht (letzte 30 Tage)
marlina
marlina am 15 Aug. 2011
Hi,
I would like to know how to calculate the number of zeros in an array and stop when the array value is one.
Example: a = [0 0 0 0 1 1 1 0 0 0]
here, there are 4 zeros before it meet array value of one.
then, let say I convert binary to RGB, 0=green and 1=red. The same thing goes here...how I want to calculate green pixel before it meet red pixel.
Anybody can give any idea. Thank you in advanced.
  1 Kommentar
marlina
marlina am 15 Aug. 2011
Let say if 0 remain black, only 1 turn to green. How to calculate black pixel before it meet the first pixel of green?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dhyun
dhyun am 15 Aug. 2011
one method you could use is to find the first position where a == 1 and then subtract 1 from it:
num_zeros = find(a == 1, 1) - 1;
  4 Kommentare
Paulo Silva
Paulo Silva am 15 Aug. 2011
find(diff(a)) %gives two index values, green to red and red to green
marlina
marlina am 15 Aug. 2011
Thanks,
I tried it but some error occurred.
Let say if 0 remain black, only 1 turn to green. How to calculate black pixel before it meet the first pixel of green?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by