필터 지우기
필터 지우기

count a sequence of 1 or 0

조회 수: 3 (최근 30일)
Boby S
Boby S 2019년 12월 10일
댓글: Boby S 2020년 2월 11일
Hi
I have the following data (2 column, A and B) imported from Excel:
A B
0 0
0 1
1 1
1 0
1 1
1 0
0 1
1 1
0 1
0 1
I want to count the repeat of 0 and 1 in sequence for column A. I do not want to count all 1 or all 0. Next, based on this count, I want to count only 0 on column B for same sequence.
I expect the following result:
2 1
4 2
1 0
1 0
2 0

채택된 답변

Stephen23
Stephen23 2019년 12월 10일
편집: Stephen23 2019년 12월 10일
>> M = [0,0;0,1;1,1;1,0;1,1;1,0;0,1;1,1;0,1;0,1]
M =
0 0
0 1
1 1
1 0
1 1
1 0
0 1
1 1
0 1
0 1
>> X = [true;diff(M(:,1))~=0];
>> Y = diff(find([X;true]));
>> V = accumarray(cumsum(X),~M(:,2));
>> Z = [Y,V]
Z =
2 1
4 2
1 0
1 0
2 0
  댓글 수: 3
Stephen23
Stephen23 2020년 2월 6일
Generate X and Y, then do this:
>> F = @(v)sum(v(1+(numel(v)>1):end));
>> V = accumarray(cumsum(X),~M(:,2),[],F);
>> Z = [Y,V]
Z =
2 0
4 2
1 0
1 0
2 1
Boby S
Boby S 2020년 2월 11일
Thank you

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by