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

更多回答(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