필터 지우기
필터 지우기

Indexing back into an array the same size as one that was just sorted

조회 수: 1 (최근 30일)
Bill
Bill 2016년 5월 27일
댓글: Bill 2016년 5월 27일
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.

채택된 답변

Walter Roberson
Walter Roberson 2016년 5월 27일
FirstN = 2;
B = A(sub2ind(size(A), repmat((1:size(A,1)).',1,FirstN), IDX(:,1:FirstN)));

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by