MATLAB - matrix sorting based on vector sorting

Possible duplicate:
How can I sort a two-dimensional array in MATLAB with respect to a single column?
Sort a matrix with a different matrix

I have a vector "A" of 429 values ​​and a matrix "B" of values ​​of 429x200. Lines A and B have the same indexes. My vector "A" contains the values ​​1: 1: 429, but they are randomly ordered throughout the vector. I want to change the order of A so that it is indexed in the order from 1 to 429, and I also want to sort the rows in the matrix "B" in the same order as the new sorted "A".

Can this be done quickly and easily without a cycle?

Here is an example to illustrate my point:

A = 5 3 1 2 4 B = 3 7 0 4 6 1 2 5 0 8 4 0 2 0 0 3 0 1 0 5 2 2 3 4 4 sortedA = 1 2 3 4 5 sortedB = 4 0 2 0 0 3 0 1 0 5 1 2 5 0 8 2 2 3 4 4 3 7 0 4 6 

Thanks everyone!

+2
sorting vector matrix matlab
source share
1 answer

Example data:

 A = [ 5, 3, 1, 2, 4 ]'; B = [ 3, 7, 0, 4, 6; 1, 2, 5, 0, 8; 4, 0, 2, 0, 0; 3, 0, 1, 0, 5; 2, 2, 3, 4, 4 ] 

Matrix Sort:

 [sortedA,IX] = sort(A); sortedB = B(IX,:); sortedA = 1 2 3 4 5 sortedB = 4 0 2 0 0 3 0 1 0 5 1 2 5 0 8 2 2 3 4 4 3 7 0 4 6 
+2
source share

All Articles