dividing a column matrix based on the no
古いコメントを表示
i have a single column matrix A=0 0 0 1 1 1 1 0 0 0 0 2 2 2 2 0 0 0 0 now i have to divide this in to several matrices such whenever 0 changes to 1 or 2 i want a new matrix in this case A1= 1 1 1 1 0 0 0 0; A2=2 2 2 2 0 0 0 0 which are also single column matrices
採用された回答
その他の回答 (1 件)
Andrei Bobrov
2011 年 5 月 16 日
more variant
A = A(:);
I = cumsum([0;diff(A)]~=0 & (A ~= 0));
out = arrayfun(@(x)A(I==x),1:max(I),'un',0);
same with the loop
A = A(:).';
I = cumsum([0,diff(A)]~=0 & (A ~= 0));
out = cell(1,max(I));
for j = 1:max(I),
out{j} = A(j==I);
end
4 件のコメント
bilhan
2011 年 5 月 16 日
bilhan
2011 年 5 月 16 日
Oleg Komarov
2011 年 5 月 16 日
I edited my solution to take into account initial conditions (if A doesn't start with a 0) and to split a matrix on the basis of the first array.
bilhan
2011 年 5 月 17 日
カテゴリ
ヘルプ センター および File Exchange で Data Type Identification についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!