MATLAB Answers

Angel Torrado

Order all the rows based on one row.

Asked by Angel Torrado
on 8 May 2013


   I´ve searching for an answer but I didn´t find any, and before programming a super inefficient function I just wanted to be sure that there weren´t any prebuilt function to do this. So the question is:

How do you sort all the rows of a matrix based on one specific row. For example, based on the first row:


         723        1081         860
           1           2           3



         723         860        1081
           1           3           2

Thank you in advance!



No products are associated with this question.

1 Answer

Answer by the cyclist
on 8 May 2013
Edited by the cyclist
on 8 May 2013
 Accepted answer

Use the sortrows() command.

You are actually sorting columns, so you will need to transpose your array, then sort, then transpose back:

sorted_L = sortrows(L',1)'

  1 Comment

That worked!! I don´t know why I didn´t think on that... ¬¬

Thank you for the super fast answer!

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!