Borrar filtros
Borrar filtros

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

1 visualización (últimos 30 días)
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!

Respuesta aceptada

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

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by