Indexing back into an array the same size as one that was just sorted
1 回表示 (過去 30 日間)
古いコメントを表示
I'm trying to first sort each row of a 3X3 array. And then take the subscripts of the two largest numbers in each row and pull them out.
So,
A = magic(3);
[~, IDX] = sort(A, 2, 'descend');
And then use IDX to pull out the largest 2 numbers in each row in array A, leaving me with and array sized 3,2.
0 件のコメント
採用された回答
Walter Roberson
2016 年 5 月 27 日
FirstN = 2;
B = A(sub2ind(size(A), repmat((1:size(A,1)).',1,FirstN), IDX(:,1:FirstN)));
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Shifting and Sorting Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!