Counting the number of runs in a sequence

I have a sequence made up a number of 1's and 0's and I want to count how many runs of numbers occur within the sequence. For example taken the matrix
A=[1,1,1,0,0,1,1,1,1,0,0,0,0,1]
The 1st run: 1,1,1 The 2nd run: 0,0 The 3rd run: 1,1,1,1 The 4th run: 0,0,0,0 The 5th run: 1,
Therefore the total number of runs is 5.
Any help would be much appreciated thanks

Respuestas (5)

Matt J
Matt J el 8 de Jul. de 2013
Editada: Matt J el 8 de Jul. de 2013
N_runs=nnz(diff(A))+1;
David Sanchez
David Sanchez el 8 de Jul. de 2013
A=[1,1,1,0,0,1,1,1,1,0,0,0,0,1];
N_runs = 1;
for k=2:length(A)
if A(k)~=A(k-1)
N_runs = N_runs +1;
end
end
Matt J
Matt J el 8 de Jul. de 2013
Editada: Matt J el 8 de Jul. de 2013
If you have the Image Processing Toolbox,
C=bwconncomp([A,~A]);
N_runs=C.NumObjects;

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Preguntada:

el 8 de Jul. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by