フィルターのクリア

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 Center および File ExchangeData Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by