MATLAB Answers

Sort matrix based on values in the first row from small to large

3 views (last 30 days)
Salad Box
Salad Box on 12 Nov 2019
Commented: Adam Danz on 13 Nov 2019
Hi,
I have a matrix M
M =
39 93 143 64 36
1 2 3 4 5
I would like to sort the matrix M by values in the top row from small to large and expecting the integers in the buttom row also move accordingly, leading to something like
newM = 36 39 64 93 143
5 1 4 2 3
I mean I could transpose the matrix and do it using 'sortrows' then transpose back.
I am just wondering whether there is a more straight forward way of achieving it?

  1 Comment

Adam Danz
Adam Danz on 13 Nov 2019
I'm curious what problems there are with sortrows. Mind sharing why you're looking for an alternative?

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 12 Nov 2019
Edited: Adam Danz on 13 Nov 2019
newM = sortrows(m.').';
That's pretty straightforward ;)

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.