フィルターのクリア

separate matrix into cells according to column value

2 ビュー (過去 30 日間)
Alejandro Fernández
Alejandro Fernández 2020 年 8 月 30 日
Hi, I have a matrix like the one I show in A:
A = [1 2 1; 2 2 1; 1 2 2; 2 2 3];
What I intend to achieve in each component of cell B is the first and second column of matrix A whose component index corresponds to the value of the third column of A.
B{1} = [1 2; 2 2];
B{2} = [1 2];
B{3} = [2 2];
I can do it with the next code but I am looking for a better and faster method because i usually have a lot of cell components.
for i = 1 : 3
B{i} = A(A(:,3) == i,1:2);
end

採用された回答

Bruno Luong
Bruno Luong 2020 年 8 月 30 日
One liner (not the fatest)
B = splitapply(@(x) {x}, A(:,1:2), A(:,3))
  1 件のコメント
Alejandro Fernández
Alejandro Fernández 2020 年 8 月 30 日
In my case is faster than my so it's perfect! thank you!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Types についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by