extracting from a matrix
1 回表示 (過去 30 日間)
古いコメントを表示
giuseppe insignito
2020 年 12 月 1 日
回答済み: Walter Roberson
2020 年 12 月 1 日
I have a matrix:
XY_indx =
1 1 1
2 1 2
1 2 3
2 2 4
1 1 5
2 1 6
1 2 7
2 1 10
1 2 11
Let's say that first and second column of the previous matrix represent coordinates, while the third is the index. I need to collect, into extra arrays, all the indexes which have same coordinates. In this case v1 = [1 5] v2 = [2 6] v3 = [3 7] ...
If simpler you can also put the couples into a single matrix, like:
v =
1 5
2 6
3 7
. .
. .
. .
thanks!
採用された回答
Walter Roberson
2020 年 12 月 1 日
XY_indx = [
1 1 1
2 1 2
1 2 3
2 2 4
1 1 5
2 1 6
1 2 7
2 1 10
1 2 11];
output = accumarray(XY_indx(:,1:2), XY_indx(:,3), [], @(V) {V})
output{2,1}
Notice that some of your groups have more than 2 entries.
This works only if the indices are positive integers, and it does not work well if the indices have large gaps (you get empty cells).
There are variations for the times those are problems:
[urow, ~, G] = unique(XY_indx(:,1:2), 'rows');
grouped = accumarray(G, XY_indx(:,3), [], @(V) {V.'});
output = [num2cell(urow,2), grouped]
Here, the first column of the cell gives the coordinates such as [2 1] that the second column of the cell refers to. The second column of the cell lists the entries such as [2, 6, 10]
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!