How to MATLAB Sort Rows

Ammar Ali Feb 02, 2024
  1. Use the sortrows() Function to Sort Matrix Rows in MATLAB
  2. Use the sortrows() Function to Sort Table Rows in MATLAB
How to MATLAB Sort Rows

This tutorial will discuss sorting rows present in a matrix using the sortrows() function in MATLAB. In data analysis and processing, sorting is essential because it will make it easy to analyze and process data when it is sorted.

For example, if we have a table of patients in a hospital and we want to find a specific person, and if the table is not sorted, we will have to go through all the data to find a person. But if the table is sorted according to the first letter of patients’ names, we can see the person easily because now we only have to look at the names starting from a specific letter.

Use the sortrows() Function to Sort Matrix Rows in MATLAB

We can use the sortrows() function of Matlab to sort rows present in a matrix. The first syntax of the sortrows() function is given below:

output = sortrows(matrix)

The above syntax will sort the rows in the given matrix according to the elements of the first column or the first element of each row in ascending order. If two or more rows have the same first element, the function will compare their second elements, and so on.

For example, let’s sort the rows in a matrix using the above syntax. See the code below.

clc
clear

My_matrix = [5:10;2:7;3:8]
New_matrix = sortrows(My_matrix)

Output:

My_matrix =

     5     6     7     8     9    10
     2     3     4     5     6     7
     3     4     5     6     7     8


New_matrix =

     2     3     4     5     6     7
     3     4     5     6     7     8
     5     6     7     8     9    10

We created a 3-by-6 matrix in the above code and sorted its rows. We can compare the two matrices in the output to check the result.

If we look at the first column of the My_matrix matrix, we can see that the second row will come first because the first element of the second row is the smallest, and the first row with the highest first value will come last in the output. We can pass a matrix of any size inside the sortrows() function.

The second syntax of the sortrows() function is given below:

output = sortrows(matrix, column)

We can use the above syntax to set the column number from the input matrix, which will be used in sorting the rows present in the given matrix. If we don’t pass the column number, the function will use the first column of the matrix for sorting rows.

We can also pass multiple column numbers in a vector, and the function will sort the rows according to the first column number. If two or more of the same values are present in the current column, the function will move to the next column number present in the given vector.

For example, let’s sort the rows of a matrix using the second and third columns for sorting. See the code below.

clc
clear

My_matrix = [5:10;3:8;7:-1:2]
New_matrix = sortrows(My_matrix,[3 4])

Output:

My_matrix =

     5     6     7     8     9    10
     3     4     5     6     7     8
     7     6     5     4     3     2


New_matrix =

     7     6     5     4     3     2
     3     4     5     6     7     8
     5     6     7     8     9    10

If we look at the third and fourth columns of the New_matrix matrix, we can see that the elements of the third and fourth columns are sorted. If we only pass a single column number with the same values present, the function will not change the position of the rows.

The third syntax of sortrows() is given below.

output = sortrows(matrix, column, direction)

In the above syntax, the direction will define the order in which we want to sort the rows, like ascend for ascending order of rows and descend for descending order of rows. By default, the order is set to ascending order.

In the case of multiple column numbers, we can also add multiple directions as a cell data type that will be used for each column.

For example, if we define two column numbers and two directions, the first column will be sorted according to the first direction. If there are the same values, the function will move to the second column and sort the rows according to the second direction.

For example, let’s sort the above matrix according to two directions. See the code below.

clc
clear

My_matrix = [5:10;3:8;7:-1:2]
New_matrix = sortrows(My_matrix,[3 4],{'ascend' 'descend'})

Output:

My_matrix =

     5     6     7     8     9    10
     3     4     5     6     7     8
     7     6     5     4     3     2


New_matrix =

     3     4     5     6     7     8
     7     6     5     4     3     2
     5     6     7     8     9    10

In the above output, when the function gets to the two same value present in the third column, which is 5, it will move to the fourth column and will use the 6 and 4 values and will sort them in descending order because the direction for the second column number is descending. Now let’s talk about sorting the rows of a table using the sortrows() function.

Use the sortrows() Function to Sort Table Rows in MATLAB

We can also use the sortrows() function to sort the rows of a table in the same way we sorted the rows of a matrix above. We can also set the variable or column we want to use for sorting and the direction or order of sorting.

For example, let’s create a table and sort its rows according to a variable. See the code below.

clc
clear

P_Name = {'Smith';'John';'Will';'Jones';'Brown'};
P_Age = [37;47;37;40;49];
P_Height = [72;68;63;67;64];

P_table = table(P_Age,P_Height,'RowNames',P_Name)
Sorted_table = sortrows(P_table,'P_Height','descend')

Output:

P_table =

  5×2 table

             P_Age    P_Height
             _____    ________

    Smith     37         72
    John      47         68
    Will      37         63
    Jones     40         67
    Brown     49         64


Sorted_table =

  5×2 table

             P_Age    P_Height
             _____    ________

    Smith     37         72
    John      47         68
    Jones     40         67
    Brown     49         64
    Will      37         63

In the above code, we created a table of patients, including their names, ages, and heights, and we sorted the table according to the height of the patients in descending order. We can see in the output that the table is sorted according to the descending order of the patients’ heights.

We can also use multiple variables and directions for sorting, just like in the case of a matrix. In the case of multiple variables, we have to pass the variable and direction names in a cell.

By default, the sortrows() function will use all the variables and ascending direction order to sort rows of the given table. We can also sort the rows according to their names using the RowNames argument in place of the variable name.

If we have missing placements in the table and want to sort the table according to the missing placements, we can use the MissingPlacement argument. After that, we can pass its value like first to place the missing value first, last to place the values at the end, and auto to place the elements first in ascending order and last in case of descending order.

We can also change the method used for comparing values using the ComparisonMethod argument. After that, we have to pass the name of the method like real for real values, abs for complex values, and auto, which will use real values in case of real input and complex values in case of complex input.

The comparison method is helpful in the case of complex values. For example, if we want to compare only the real part of the complex values, we can use the real method, and if we wish to take the absolute of the complex values, we can use the abs method.

We can also get the index from the sortrows() function, which shows the rearrangement of rows of the input matrix or table. The syntax of the above properties is given below.

[Sorted_table, index] = sortrows(P_table,'RowNames','MissingPlacement','last','ComparisonMethod','abs')

If we want to sort a specific number of rows from the input matrix or table, we have to store them in a separate matrix or table and then use the sortrows() function and store the result back in the original table or matrix. Check this link for more details about the sortrows() function.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - MATLAB Matrix