How do I gain indices of max values in 2D matrix?
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
For a 16 x 12 matrix I should sort it in descending order to gain the max values (like 10 in total). After that, I should find what was the indices of those max values before sorting .
I tried several times with sort and find. But it doesn't give me correct result.
2 commentaires
Azzi Abdelmalek
le 8 Avr 2014
Modifié(e) : Azzi Abdelmalek
le 8 Avr 2014
You have two columns, you have to precise how you want to sort them
Azzi Abdelmalek
le 8 Avr 2014
You have a 16x12 matrix, there are several ways to sort your matrix, by rows or by columns? you have to give more details, or just show the result for a short example, if
A=[1 2;3 4;0 1;4 5 ;100 20;2 2;0 7]
What should be the result?
Réponses (3)
Walter Roberson
le 8 Avr 2014
[sorted, idx] = sort(YourMatrix, 'rows', 'descend');
tenmax = idx(1:10);
2 commentaires
Walter Roberson
le 8 Avr 2014
[sorted, idx] = sortrows(YourMatrix, -(1:size(YourMatrix,2)));
tenmax = idx(1:10);
this will give you the indices of the original rows that got sorted to the corresponding row position.
Gautam
le 10 Avr 2014
The answer above gives you the indices of before sorting. Try this example.
A = [1 4; 6 2; 4 2; 7 3]
[sortedMat,sortedIndex] = sort(A,'descend')
The sortedIndex matrix gives indices of the original rows (Note here that each column is sorted independently)
0 commentaires
Jos (10584)
le 10 Avr 2014
Why sort first?
A = [1 4; 6 2; 4 2; 7 3]
[maxValues, RowIndex] = max(A,[],1)
LinearIndex = sub2ind(size(A),RowIndex, 1:size(A,2))
sortedA = sort(A,1,'descend')
0 commentaires
Voir également
Catégories
En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!