create cell array based on unique combinations in two columns

2 ビュー (過去 30 日間)
merialu
merialu 2018 年 11 月 6 日
コメント済み: merialu 2018 年 11 月 7 日
Hi!
i have a matrix with 3 columns and the first two columns contains x- and y coordinates.
looks something like this [1 1 1, 1 1 2, 2 1 1, 1 2 1, 2 1 6......]
I want to create a cell array based on the content in the two first columns, more precise i want each cell to contain all values in column 3 that has the same values in column 1 and 2.
I have managed to do it based on the value in one columns:
X=arrayfun(@(x) data(data(:,1)==x, :), unique(data(:,1)),'uniformoutput', false);
the problem is that the same x coordinate (column 1) can have several y coordinates (column 2).
so my question is: how can a create a cell array based on the unique combination of two columns?

採用された回答

Bruno Luong
Bruno Luong 2018 年 11 月 6 日
data=ceil(4*rand(20,3))
[m,n] = size(data);
B = sortrows(data,[1 2]);
G = mat2cell(B,diff([0; find(any(diff(B(:,[1 2])),2)); m]),n);
G{:}
  1 件のコメント
merialu
merialu 2018 年 11 月 7 日
Thank you Bruno, This works perfectly :)

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

その他の回答 (1 件)

Bruno Luong
Bruno Luong 2018 年 11 月 6 日
data=ceil(4*rand(20,3))
[~,~,J] = unique(data(:,[1,2]),'rows');
G = accumarray(J(:),(1:length(J)),[],@(r) {data(sort(r),:)});
G{:}

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by