How to sort based on two columns

2 次查看(过去 30 天)
SHUNBAGA PRADEEPA T
回答: Jon 2020-10-5
col1
42.8525 2 0.1475
15.4135 1 0.4135
17.9379 1 1.9379
43.7166 2 3.2834
41.1387 2 5.8613
39.0441 2 8.9559
49.2551 1 8.7449
29.5191 2 1.5191
40.7013 2 2.2987
35.2668 2 7.7332
35.8608 2 5.8608
22.4636 1 2.4636
51.207 1 8.793
32.5034 2 3.5034
42.3146 2 5.6854
16.6997 1 4.6997
I want the sort col 2 in ascending order i.e all 1's first and based on this in col 1 also i was it in ascending order,
then next all 2's in col 2 and accordingly col 1 to be sorted in ascending, corresponding value of col 3 is to be updated
I expect the output as follows
15.4135 1 0.4135
16.6997 1 4.6997
17.9379 1 1.9379
22.4636 1 2.4636
49.2551 1 8.7449
51.207 1 8.793
29.5191 2 1.5191
32.5034 2 3.5034
35.2668 2 7.7332
35.8608 2 5.8608
39.0441 2 8.9559
40.7013 2 2.2987
41.1387 2 5.8613
42.3146 2 5.6854
42.8525 2 0.1475
43.7166 2 3.2834

回答(1 个)

Jon
Jon 2020-10-5
You should be able to use sortrows for this. Please type doc sortrows on the command line for further documentation

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by