Split vector every time data changes from 2 to 1
1 回表示 (過去 30 日間)
古いコメントを表示
I have one vector filled with 1 and 2 as follows:
Vector A:
1
1
1
1
2
2
2
1
1
2
2
1
1
1
2
2
1
1
2
2
I want to split the above vector everytime the value change the 1, then I will have:
Vector B Vector C Vector D Vector E
1 1 1 1
1 1 1 1
1 2 1 2
1 2 2 2
2 2
2
2
Thank you
0 件のコメント
採用された回答
Walter Roberson
2019 年 3 月 4 日
vectors = mat2cell(A, diff([0;find(A(1:end-1)==2 & A(2:end) == 1); length(A)]),1);
0 件のコメント
その他の回答 (2 件)
Jos (10584)
2019 年 3 月 4 日
Another approach:
A = [1 2 1 1 2 1 2 2 1 1 1 2 2 2 1 1 1 2].' % data
C = accumarray(cumsum([0 ; diff(A)==-1])+1, A, [], @(x) {x}) % engine
isequal(A,cat(1,C{:})) % check
0 件のコメント
KSSV
2019 年 3 月 4 日
A = [1
1
1
1
2
2
2
1
1
2
2
1
1
1
2
2
1
1
2
2] ;
idx = find(diff(A)==-1) ;
idx = [0 ; idx ;length(A)] ;
N = length(idx)-1 ;
iwant = cell(N,1) ;
for i = 1:N
iwant{i} = A(idx(i)+1:idx(i+1)) ;
end
celldisp(iwant)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Numeric Types についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!