필터 지우기
필터 지우기

Index of repeated values

조회 수: 3 (최근 30일)
b
b 2021년 12월 22일
댓글: b 2021년 12월 22일
For the binary vectors of the following type:
ad=[1 1 1 1 0 0 1 1 1 1]
ad=[0 0 1 1 1 1 0 0 1 1]
ad=[0 1 0 1 1 1 1 1 0 0]
ad=[1 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0]
suppose we are interested in finding the index values of the ad vector with consecutive 1s (or 0s). Without using mex, how can we obtain the following output:
For the first case,
output1=[1 2 3 4]; output2=[7 8 9 10];
For the second case,
output1=[3 4 5 6]; output2=[9 10];
For the third case,
output=[4 5 6 7 8];
For the fourth case,
output1=[5 6 7]; output2=[12 13 14];

채택된 답변

KSSV
KSSV 2021년 12월 22일
ad=[1 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0] ;
ad0 = zeros(size(ad)) ;
ad0(logical(ad)) = find(ad) ;
ii = zeros(size(ad0));
jj = ad0 > 0;
ii(strfind([0,jj(:)'],[0 1])) = 1;
idx = cumsum(ii).*jj;
out = accumarray( idx(jj)',ad0(jj)',[],@(x){x'});
celldisp(out)
out{1} = 1 out{2} = 5 6 7 out{3} = 12 13 14
  댓글 수: 1
b
b 2021년 12월 22일
Many thanks.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by