Sorting data in Matlab

While you are often working with matrices (and vectors) in Matlab, you will come across situations where sorting the data will become a necessity. Here are some tips for how to go about doing so.

Lets begin with a predefined matrix A.

A =
     1     4     5
     3     4     6
     3     2     7
     2     8     5

The function “sort” will allow you to order the columns or rows in the array A. The sort command can take in three arguments, and return two values:

[A_sorted sorted_index] = sort(A,dimension,sort_order);

A represents the unsorted array and is the one required argument. The dimension equals 1 or 2, with values equaling column sorting or row sorting, respectively. By default sort will order values along each column. The sort_order can equal “descend” or “ascend”, where the default is ascending order. A_sorted returns the sorted array. Finally, sorted_index is the index or original ordering of each value A.

For example:

[A_sorted sorted_index] = sort(A)

would return:

A_sorted =
     1     2     5
     2     4     5
     3     4     6
     3     8     7

sorted_index =
     1     3     1
     4     1     4
     2     2     2
     3     4     3

The index can be very useful in cases where you want to sort all columns based on a single column, while keeping each row intact. Therefore if you wanted to sort A by column 2, while maintaining row integrity across columns 1 and 3, you can use the following:

A_sorted(:,1) = A(sorted_index(:,2),1);
A_sorted(:,3) = A(sorted_index(:,2),3);

The result would be:

A_sorted =
     3     2     7
     1     4     5
     3     4     6
     2     8     5

Sometimes you want to maintain integrity in all the rows, but sort data first by one column, and if there are any equal values, sort by another column. To do this, use the command sortrows:

[A_sorted2 sorted_index2] = sortrows(A,[1 2])

This tells sortrows to sort array A by column 1 first, then 2. And again we can return the sorted array as well as the indices of the original rows in A.

A_sorted2 =
     1     4     5
     2     8     5
     3     2     7
     3     4     6

sorted_index2 =
     1
     4
     3
     2

To perform a descending sort using sortrows utilize the (-) sign when designating columns. For example, to sort A in ascending order first on column 3 and then in descending order on column 1, run the following:

sortrows(A,[3 -1])
ans =
     2     8     5
     1     4     5
     3     4     6
     3     2     7

As you can see, column 3 is sorted in ascension, and in the case where two 5’s occur, the next sort will reorder based on descending values in column 1.

The use of sort is not relegated to integers as shown here, but can also be used with real and imaginary numbers as well as strings. If you have any further suggestions for sorting data, let us know.

7 thoughts on “Sorting data in Matlab

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.