Updated March 10, 2023
Introduction to Matlab 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
- X = sort (Y)
- X = sort (Y, dimension)
- X = sort (Y, direction)
Description:
- 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.
- 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.
- 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:
- Initialize the input matrix
- Pass the input matrix as an argument to sort function
Code:
Y = [4 1 5; 4 -2 0; 1 5 -3]
X = sort(Y)
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:
Input:
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:
- Initialize the input matrix
- Pass the input matrix as an argument to sort function
- Pass ‘2’ as the second argument to the sort function
Code:
Y = [8 1 10; -4 7 3; 5 5 2]
X = sort(Y, 2)
This is how our input and output will look like in the MATLAB command window:
Input:
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:
- Initialize the input matrix
- Pass the input matrix as an argument to sort function
- Pass ‘descend’ as the second argument to the sort function
Code:
Y = [5 1 12 2; 4 2 3 7; 13 5 12 4]
X = sort(Y, ‘descend’)
This is how our input and output will look like in the MATLAB command window:
Input:
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:
- Initialize the input matrix with complex elements
- Pass the input matrix as an argument to sort function
Code:
Y = [4+2i 3+2i; 1i -2i];
X = sort(Y)
This is how our input and output will look like in the MATLAB command window:
Input:
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.
Conclusion
1. Sorting can be performed in MATLAB using the sort function
2. By default, the sort function sorts the matrix along with its columns and in the ascending order
3. We can change the default properties of the sort function using different types of arguments to get the sorting order as per our requirement
Recommended Articles
This is a guide to the Matlab sort matrix. Here we discuss the Examples of the Matlab sort matrix along with the codes, inputs, and outputs. You may also have a look at the following articles to learn more –