EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab sort matrix
 

Matlab sort matrix

Updated March 10, 2023

Matlab sort matrix

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

Input:

Matlab sort matrix output 1

Output:

Matlab sort matrix output 1.2

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:

Input:

Matlab sort matrix output 2

Output:

output 2.2

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:

Input:

output 3

Output:

output 3.2

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:

Input:

output 4

Output:

output 4.2

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 –

  1. Jacobian Matlab
  2. Polyval MATLAB
  3. Cell to String MATLAB
  4. Strcmp Matlab

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW