Sort rows of a matrix based on a specific array
Afficher commentaires plus anciens
Hi everyone,
Let us assum Matrix A and array B as follows:
A = [1 0 1
2 0 0
4 0 0
3 1 1
6 0 0
5 0 0]
B = [3 4 1]
Array B always include values stored in the first column of Matrix A. So, I want to sort rows of Matrix A based on Array B. Whatever comes first in Array B must come first in Matrix A. Moreover, I do not want to change the position of any other rows in Matrix A that their values (first column) do not exist in Array B.
Having said this, sorted version of Matrix A based on Array B will be as follows:
An = [3 1 1
2 0 0
4 0 0
1 0 1
6 0 0
5 0 0]
Would you please guide me what is the fastest way to do this?
Regards,
Amir
Réponse acceptée
Plus de réponses (1)
madhan ravi
le 13 Juil 2020
v = sort(B);
A([1, v(end)], :) = A([v(end), 1], :)
2 commentaires
Amirhossein Moosavi
le 13 Juil 2020
madhan ravi
le 13 Juil 2020
I misunderstood the question.
Catégories
En savoir plus sur Shifting and Sorting Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!