Indexing back into an array the same size as one that was just sorted
2 次查看(过去 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!