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

조회 수: 3 (최근 30일)
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!

채택된 답변

Titus Edelhofer
Titus Edelhofer 2012년 6월 14일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by