I am trying to sort the columns of a matrix based on the another vector values in a
fast way in matlab.
Let assume I have a reference vector:
a = [1 8 3];
And the matrix I wanna sort by rows
b =[2 3 4
5 6 4
1 2 1];
And I want the result based on the vector a if the sorted a is
a = [8 3 1]
the sorted matrix will be now
b = [3 4 2
6 4 5
2 1 1];
So the index of the first vector is the same with the index of the columns
So we sort columns based on the values of the vector.
In Matlab the
sortfunction returns two variables, the first is the sorted vector/matrix and the second is a set of indices that can be combined with the original data to produce the sorted values.A_sorted1andA_sorted2are equivalent.If the number of rows in
bis equal to the number of elements inathen you can simply use:And if you want to sort by columns use: