How to count the number of 1 between two 0 in a vector?
Mostra commenti meno recenti
I have this vector:
[0 1 1 1 0 0 1 0 1 1 0 1]
I want to create a vector that every time that I have 1 or more consecutive 1 it counts the number of 1 between two zero. So the results must be:
[3 1 2 1]
How can I do that?
thanks
Risposta accettata
Più risposte (1)
Sean de Wolski
il 3 Feb 2020
[regionprops(bwlabel([0 1 1 1 0 0 1 0 1 1 0 1]),'Area').Area]
3 Commenti
I was also thinking about using bwlabel for those who have the Image Processing Toolbox.
A = [0 1 1 1 0 0 1 0 1 1 0 1];
B = histcounts(bwlabel([0,A(:).',0]),'BinMethod','integer');
B(1) = [];
Sean de Wolski
il 4 Feb 2020
A vector is just a long and skinny image! I find things like bwlabel, bwareaopen, etc. to often be useful for this kind of thing.
Adam Danz
il 4 Feb 2020
Agreed - I'm relatively new to the bw.... functions but I use bwlabel often. Since it requires access to a toolbox I went with a lower level solution in my answer.
Categorie
Scopri di più su Operators and Elementary Operations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!