フィルターのクリア

Separte Matrix depending on changing value in column

1 回表示 (過去 30 日間)
Dom
Dom 2021 年 10 月 14 日
編集済み: Voss 2021 年 10 月 14 日
Hi!
I got the following problem. I got a (N,2) Matrix. Now I want to cut the Matrix in severval (n,2) matrices with N>=n, depending on a changing value in the second column. For example [a,1; b,2; c,2; d,1] sould be separated into 3 matrices [a,1] [b,2 c,2] [d,1].
Thanks!

採用された回答

Voss
Voss 2021 年 10 月 14 日
編集済み: Voss 2021 年 10 月 14 日
Let X be your N-by-2 matrix. Then the following code will separate X into a cell array Y, each element of which is a slice of X according to the second column value:
idx = 1+[0; find(diff(X(:,2))); size(X,1)];
n = numel(idx)-1;
Y = cell(n,1);
for i = 1:n
Y{i} = X(idx(i):idx(i+1)-1,:);
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by