JIYIK CN >

Current Location:Home > Learning > PROGRAM > MATLAB >

MATLAB sorts the rows

Author:JIYIK Last Updated:2025/04/18 Views:

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

For example, if we have a table of patients at a hospital and we want to find a specific person, if the table is not sorted, we will have to go through all the data to find the person. But if the table is sorted by the first letter of the patient's name, we can easily find the person because now we only need to look at the name starting with a specific letter.


sortrows()Use the function to sort matrix rows in MATLAB

We can use Matlab's sortrows()function to sort the rows present in a matrix. sortrows()The first syntax of the function is as follows:

output = sortrows(matrix)

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

For example, let us 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×6 matrix in the code above and sorted its rows. We can compare the two matrices in the output to check the result.

If we look at My_matrixthe first column of the matrix, we can see that the second row will be output first because the first element of the second row is the smallest, and the first row with the largest first value will be output last. We can sortrows()pass matrices of any size in the function.

sortrows()The second syntax of the function is as follows:

output = sortrows(matrix, column)

We can use the above syntax to set the column number of the input matrix, which will be used to sort the rows present in the given matrix. If we do not pass the column number, the function will use the first column of the matrix to sort the rows.

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

For example, let us sort the rows of a matrix using the second and third columns. 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 New_matrixthe third and fourth columns of the 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 value, the function will not change the position of the rows.

sortrows()The third syntax of is shown below.

output = sortrows(matrix, column, direction)

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

In case of multiple column numbers, we can also add multiple directions as the 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 identical values, the function will move to the second column and sort the rows according to the second direction.

For example, let us sort the above matrix in 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 reaches the two identical values ​​present in the third column i.e. 5, it moves to the fourth column and uses the values ​​of 6 and 4 and sorts them in descending order as the direction of the second column numbering is descending. Now let us talk about sortrows()sorting the rows in a table using the function.


sortrows()Use the function to sort table rows in MATLAB

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

For example, let us create a table and sort its rows based on 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 have created a table of patients including their name, age and height and we have sorted the table based on the height of the patients in descending order. We can see in the output that the table is sorted in descending order of the height of the patients.

We can also sort using multiple variables and directions, just like in case of matrix. In case of multiple variables, we have to pass the variable and direction name in one cell.

By default, sortrows()the function will sort the rows of a given table using all variables and in ascending order. We can also use RowNamesthe parameter in place of the variable name to sort the rows based on their names.

If we have missing placements in the table and want to sort the table based on the missing placements, we can use MissingPlacementthe -p parameter. After that, we can pass its value like firstput missing values ​​first, lastput values ​​at the end, autoput elements first in ascending order, and put elements last in descending order.

We can also ComparisonMethodchange the method used to compare values ​​using the parameter. Afterwards, we have to pass the name of the method, such as real-valued real, complex-valued absand auto, which will use real values ​​in case of real inputs and complex values ​​in case of complex inputs.

Comparison methods are helpful in case of complex values. For example, if we only want to compare the real parts of complex values, we can use realthe method, and if we want to take the absolute value of a complex value, we can use absthe method.

We can also sortrows()get the indices from the function which shows the rearrangement of the rows of the input matrix or table. The syntax of the above attribute is shown below.

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

If we want to sort a specific number of rows from an input matrix or table, we have to store them in a separate matrix or table and then use sortrows()the sort function and store the result back into the original table or matrix.

Previous: None

Next: None

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

如何在 Matplotlib Pyplot 中显示网格

Publish Date:2024/02/04 Views:145 Category:Python

本文演示了如何在 Python Matplotlib 中在一个图上画一个网格。使用 grid()函数来绘制网格,并解释了如何改变网格颜色和线条类型。

如何在 Matplotlib 中画一条任意线

Publish Date:2024/02/04 Views:168 Category:Python

本教程讲解了我们如何在 Matplotlib 中使用 matplotlib.pyplot.plot()、matplotlib.pyplot.vlines()、matplotlib.pyplot.hlines()方法和 matplotlib.collection.LineCollection 绘制任意线条。

Matplotlib 中的叠加条形图

Publish Date:2024/02/04 Views:185 Category:Python

本教程展示了如何使用 plt.bar()方法将某些数据集的条形图堆叠在另一个数据集上。我们在 Matplotlib 中使用 matplotlib.pyplot.bar()方法生成条形图。

设置 Matplotlib 网格间隔

Publish Date:2024/02/04 Views:261 Category:Python

本教程将介绍我们如何在 Matplotlib 绘图中设置网格间距,并对主要网格和次要网格应用不同的样式。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial