How to sort a matrix based on the values of one column in Matlab?

In this article, we propose a simple way for sorting a matrix depending on the values of one of its columns. We use a minimal example for that but the method can be applied to matrices of any size.

Let's create our matrix first (it will be an unsorted 3x2 matrix). You may skip Steps 1 to 3 as you will have a matrix already!

  • Step 1: Create  the 1st column of the matrix

Matlab - How to sort a matrix depending on a column

  • Step 2: Create the 2nd column of the matrix

Matlab - How to sort a matrix depending on a column

  • Step 3: Compose the two columns to form the matrix

Matlab - How to sort a matrix depending on a column

  • Step 4: Sort the target column (in our example it is the 2nd column) of the matrix and receive a sorter vector

Matlab - How to sort a matrix depending on a column

  • Step 5: Compose the SORTED matrix

Matlab - How to sort a matrix depending on a column

 

I hope this helps somebody!!!

It helped me, THANKS!

It helped me, THANKS!

Worked!

Thanks for the help

Thanks a lot!

Thanks a lot!

Thank you!  This example has

Thank you!  This example has to be included in Matlab Help!

Thanks a Lot!!!!!!!!!!!!!

Thanks a Lot!!!!!!!!!!!!! It worked

ALTERNATIVE

there is the alternative sortrows who does exactly the same. Everything you explained sofar is embedded in this command!
maybe it's worthwile to check it out!

GL!

It works and saved a lot of

It works and saved a lot of time.Thanks.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.