Finding the numbers of a vector with the same length?
Mostrar comentarios más antiguos
Dear all
Assuming there is a vector consisting only 0 and 1 like:
x=[0 1 1 0 1 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 1 1 0 1]
I want to figure out how many times 1 or 1 1 or 1 1 1 or even longer 1 in a row exists are in this vector?
The solution here would be:
nr_1= 3
nr_1_1= 3
nr_1_1_1= 2
nr_1_1_1_1 = 0
....
Is there a easy way to do it?
Thanks for your help
(Matlab 2010a)
Respuesta aceptada
Más respuestas (2)
Here's another solution:
D = diff(find([1 ~x 1]))-1;
D = histc(D,1:max(D))
Harshit
el 14 de Nov. de 2012
0 votos
Hi,
You can save zero as it is and whenever 1 appears start a counter and count the number of 1s in succession. It is Run length encoding. When done just count the number of times the numbers appears.
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!