MATLAB Answers

How to sort a matrix in desceding order?

1 view (last 30 days)
SM
SM on 9 Feb 2020
Commented: SM on 9 Feb 2020
I have a matrix such as:
A =[
0.8604 0.5134 0.9391 0.6482 0.3479 0.3308 0.7069 0.7640 0.0567 0.9052
0.9344 0.1776 0.3013 0.0252 0.4460 0.8985 0.9995 0.8182 0.5219 0.6754
0.9844 0.3986 0.2955 0.8422 0.0542 0.1182 0.2878 0.1002 0.3358 0.4685
0.8589 0.1339 0.3329 0.5590 0.1771 0.9884 0.4145 0.1781 0.1757 0.9121
0.7856 0.0309 0.4671 0.8541 0.6628 0.5400 0.4648 0.3596 0.2089 0.1040];
if i use
[~, sortOrder] = sort(A(1,:), 'descend')
then it gives
sortOrder=[3 10 1 8 7 4 2 5 6 9];
but if i apply this code for all rows such as
[~, sortOrder] = sort(A(:), 'descend')
it shows results which i dont want.
How can i solve it if i want the following outcome:
sortOrder=
[3 10 1 8 7 4 2 5 6 9
7 1 6 8 10 9 5 3 2 4
1 4 10 2 9 3 7 6 8 5
6 10 1 4 7 3 8 5 9 2
4 1 5 6 3 7 8 9 10 2];
No for loop please! I use R2017b.
Thanks in advance!

Accepted Answer

Walter Roberson
Walter Roberson on 9 Feb 2020
[~, sortOrder] = sort(A, 2, 'descend');

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by