Count number of repeated element before next different number in array?

조회 수: 1 (최근 30일)
S. Perez
S. Perez 2019년 1월 14일
댓글: S. Perez 2019년 1월 14일
Hi,
This is the problem:
I have an array of size 1xN, and this array is filled with 1 and 0. What I want is to count the number of repeated ones before a zero.
I will try to explain with an example.
A = [1,1,1,0,0,0,0,1,0,0,1,1,0,0,0,1,1,1,1];
What I want the code to do is to count how many times the ones are repeated before a zero. So, the answer is
OnesRepeated = [3, 1, 2, 4]
Can anyone help please? Thanks

채택된 답변

Stephen23
Stephen23 2019년 1월 14일
편집: Stephen23 2019년 1월 14일
>> D = diff([0,A==1,0]);
>> find(D<0)-find(D>0)
ans =
3 1 2 4

추가 답변 (1개)

Adam
Adam 2019년 1월 14일
runs = diff( [0 find( diff( A ) ) numel( A )] )
if A(1) == 1
OnesRepeated = runs( 1:2:end );
else
OnesRepeated = runs( 2:2:end );
end
  댓글 수: 1
Rik
Rik 2019년 1월 14일
Alternatively, you could use RunLength or search the FEX for other implementations.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by