Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
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!

Akzeptierte Antwort

Titus Edelhofer
Titus Edelhofer am 14 Jun. 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

Weitere Antworten (0)

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by