Effacer les filtres
Effacer les filtres

Sort matrix A from least to greatest and have corresponding entries in matrix B move to equal positions

1 vue (au cours des 30 derniers jours)
I am writing a program, and need it to sort one matrix, A, from least to greatest (which I know how to do). However, I also need the corresponding entries in matrix B to move to the same positions as those in A. Entries in matrix B are identifiers for something that I will need to use later in the program, and I am only interested in the one that goes with the smallest value from A. Is there a simple way to do this (like combining A and B, maybe sorting the columns?)? Thank you for the help!

Réponse acceptée

Titus Edelhofer
Titus Edelhofer le 14 Juin 2012
Hi,
there are two ways: either combine them into one matrix and use sortrows, the other is to sort A and call sort with two output arguments. The second output argument is the reordering, i.e., so either
AB = sortrows([A B]);
or
[A, idx] = sort(A);
B = B(idx);
(both assuming that your "matrices" A and B are column vectors).
Titus

Plus de réponses (0)

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