How can I number each element of a matrix according to the corresponding ordered position, row by row?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Daniela Conforti
le 8 Jan 2019
Modifié(e) : Daniela Conforti
le 8 Jan 2019
For example, I have matrix A:
A = [9 15 1 16
15 9 1 16
1 6 2 4]
The sorted matrix is:
B= [1 9 15 16
1 9 15 16
1 2 4 6]
For each rows of A, I want to determine if each element corresponds to the first, second, third or fourth position, as follows:
A'= [2 3 1 4
3 2 1 4
1 4 2 3]
Thanks for help
0 commentaires
Réponse acceptée
Andrei Bobrov
le 8 Jan 2019
Modifié(e) : Andrei Bobrov
le 8 Jan 2019
[~,ii] = sort(A,2);
[~,out] = sort(ii,2)
1 commentaire
Plus de réponses (1)
madhan ravi
le 8 Jan 2019
Modifié(e) : madhan ravi
le 8 Jan 2019
Note: Your A' doesn't correspond to the exact position. See https://www.mathworks.com/help/matlab/ref/sort.html#bt8nojg-1-I
A = [9 15 1 16 ;...
15 9 1 16 ;...
1 6 2 4 ];
Adash = [2 3 1 4 ;...
3 2 1 4 ;...
1 4 2 3];
[B,I]=sort(A,2) % B - sorted matrix , I - position of the original values --> see doc sort for explanation
Gives:
B =
1 9 15 16
1 9 15 16
1 2 4 6
I =
3 1 2 4
3 2 1 4
1 3 4 2
To compare A' you could do somethig like below:
Adash==I
Gives:
ans =
3×4 logical array
0 0 0 1
1 1 1 1
1 0 0 0
Voir également
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!