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

1 vue (au cours des 30 derniers jours)
Bill le 27 Mai 2016
Commenté : Bill le 27 Mai 2016
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 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Walter Roberson le 27 Mai 2016
FirstN = 2;
B = A(sub2ind(size(A), repmat((1:size(A,1)).',1,FirstN), IDX(:,1:FirstN)));
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Bill le 27 Mai 2016
That does it. Thank you!

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by