Extracting rows from a matrix based on values in columns

Following on from this question
What if I have more than 1 column? That is, I want A to be a 6-by-4 matrix for example, so to have C{1} = 4-by-3 matrix?

 採用された回答

Guillaume
Guillaume 2017 年 10 月 8 日

1 投票

It's much easier to do this with the newer splitapply rather than the older accumarray:
A = [[1;1;3;2;3;1], rand(6, 3)]
C = splitapply(@(rows) {rows}, A(:, 2:end), A(:, 1))

2 件のコメント

Cedric
Cedric 2017 年 10 月 8 日
I am up-voting even though ...
;)
Maria445
Maria445 2017 年 10 月 8 日
Awesome, thanks!

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

その他の回答 (1 件)

Kaushik Lakshminarasimhan
Kaushik Lakshminarasimhan 2017 年 10 月 8 日

1 投票

for n=2:4, C_temp(:,n-1) = accumarray(A(:,1),A(:,n),[],@(n){n}); end
for m=1:size(C_temp,1), C{m} = cell2mat(C_temp(m,:)); end

カテゴリ

ヘルプ センター および File ExchangeMathematics and Optimization についてさらに検索

タグ

質問済み:

2017 年 10 月 8 日

コメント済み:

2017 年 10 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by