From: http://kb.iu.edu/data/afrd.html
In Matlab, how can I sort all of the rows of a matrix according to one of the columns?
If you wish to sort all the rows of a matrix in Matlab according to one of the columns, the general syntax for the command to use is:
[OldRowNumber, NewRowNumber] = sort(OldMatrix(:,SortColumn)); NewMatrix = OldMatrix(NewRowNumber,:);
Suppose, for example, you would like to rearrange the example matrix below (Figure 1) according to the value of the third column, so that your results would be as shown in Figure 2:
Figure 1
A = [1 2 3 3 0 9 6 5 4]
Figure 2
B = [1 2 3 6 5 4 3 0 9]
You would use the following command:
[x, i] = sort(A(:,3)); B = A(i,:);