Categories

Sort matrix

In MATLAB, sorting can be used to arrange the elements of an array in the required direction, i.e. ascending order or descending order. We can use the sort function in MATLAB with various arguments to sort the columns or rows as per our requirement. Starting from the ‘R2017a’ version of MATLAB, we can also sort elements stored in the form of strings using the sort function, which sorts the string as per the Unicode dictionary. In this topic, we are going to learn about the Matlab sort matrix.

The syntax for sorting matrix in Matlab

1. X = sort (Y)
2. X = sort (Y, dimension)
3. X = sort (Y, direction)

Description:

1. X = sort (Y) is used to sort the elements of Y in the ascending order. If input Y is a vector, the function sort (Y) will sort the elements of the vector Y. If input Y is a matrix, the function sort (Y) will treat the columns as vectors & will sort each column.
2. X = sort(Y, dimension) is used if we need to sort the elements of matrix Y along the rows. Here we can pass ‘2’ as an argument, which represents rows.
3. X = sort (Y, direction) is used to decide the direction of the sorting order. By default, the direction is ‘Ascending’, which can be changed to ‘Descending’, bypassing the argument ‘descend’. Similarly, ‘ascend’ can be passed to get the sorting order in ascending order.

Examples of Matlab sort matrix

Let us now understand the code to perform sorting in MATLAB.

Example 1:

In the first example, we will take a 3 x 3 matrix and will sort it using default properties of the sort function (Ascending order and along with the columns).

For our first example, we will follow the following steps:

1. Initialize the input matrix
2. Pass the input matrix as an argument to sort function

Code:

``Y = [4 1 5; 4 -2 0; 1 5 -3]``

[Initializing the elements of the 3 x 3 matrix]

`X = sort(Y)`[Passing the input matrix to the sort function]

Since we have not passed any argument other than the input matrix, the elements will be sorted by the default property of the sort function, i.e., along with the columns and in the ascending order.

This is how our input and output will look like in the MATLAB command window:

Output:

As we can see in the output, the elements of the input matrix are sorted along with the columns and in the ascending order.

Example 2:

In this example, we will take a 3 x 3 matrix and will sort it along the rows. For this, we will pass ‘2’ as an additional argument, which represents ‘rows’.

For this example, we will follow the following steps:

1. Initialize the input matrix
2. Pass the input matrix as an argument to sort function
3. Pass ‘2’ as the second argument to the sort function

Code:

``Y = [8 1 10; -4 7 3; 5 5 2]``

[Initializing the elements of the 3 x 3 matrix]

`X = sort(Y, 2)`[Passing the input matrix to the sort function. Also, the second argument ‘2’ is passed for sorting the rows]

This is how our input and output will look like in the MATLAB command window:

Output:

As we can see in the output, the elements of the input matrix are sorted along the rows and in the ascending order. Please note that the order is ascending as per the default property of the sort function.

Example 3:

In this example, we will take a 4 x 4 matrix and will sort it in descending order. For this, we will pass ‘descend’ as an additional argument, which represents the descending order.

For this example, we will follow the following steps:

1. Initialize the input matrix
2. Pass the input matrix as an argument to sort function
3. Pass ‘descend’ as the second argument to the sort function

Code:

``Y = [5 1 12 2; 4 2 3 7; 13 5 12 4]``

[Initializing the elements of the 4 x 4 matrix]

`X = sort(Y, ‘descend’)`[Passing the input matrix to the sort function. Also, the second argument ‘descend’ is passed for sorting the elements in descending order]

This is how our input and output will look like in the MATLAB command window:

Output:

As we can see in the output, the elements of the input matrix are sorted along with the columns and in the descending order.

Example 4:

In this example, we will learn how to sort a complex matrix. The sort function will sort the elements using the real parts. In case the elements have equal real parts, the sort function will use the imaginary part to decide the sorting order.

For this example, we will follow the following steps:

1. Initialize the input matrix with complex elements
2. Pass the input matrix as an argument to sort function

Code:

``Y = [4+2i 3+2i; 1i -2i];``

[Initializing the elements of the complex matrix]

`X = sort(Y)`[Passing the input complex matrix to the sort function]

This is how our input and output will look like in the MATLAB command window:

Output:

As we can see in the output, the elements of the complex matrix are sorted along with the columns and in the ascending order. Also, the sorting is done based on the real part of the elements.