필터 지우기
필터 지우기

Counting the number of runs in a sequence

조회 수: 7 (최근 30일)
olig
olig 2013년 7월 8일
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

답변 (5개)

Matt J
Matt J 2013년 7월 8일
편집: Matt J 2013년 7월 8일
N_runs=nnz(diff(A))+1;

Azzi Abdelmalek
Azzi Abdelmalek 2013년 7월 8일
numel(strfind(A,[0,1]))+ numel(strfind(A,[1,0]))+1

Jan
Jan 2013년 7월 8일

David Sanchez
David Sanchez 2013년 7월 8일
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
  댓글 수: 1
Jan
Jan 2013년 7월 8일
Vectorized:
N_runs = sum(A(2:end) ~= A(1:end-1));

댓글을 달려면 로그인하십시오.


Matt J
Matt J 2013년 7월 8일
편집: Matt J 2013년 7월 8일
If you have the Image Processing Toolbox,
C=bwconncomp([A,~A]);
N_runs=C.NumObjects;

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by