Azzera filtri
Azzera filtri

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

1 visualizzazione (ultimi 30 giorni)
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!

Risposta accettata

Titus Edelhofer
Titus Edelhofer il 14 Giu 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

Più risposte (0)

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by