1
votes

Suppose I have the following array

1.0000    2.0000    0.4231
1.0000    3.0000    0.8050
1.0000    4.0000    0.7116
2.0000    4.0000    0.6682
2.0000    5.0000    0.4460
2.0000    7.0000    0.5818
3.0000    4.0000    0.0977
3.0000    6.0000    0.5989
3.0000    7.0000    0.3237
4.0000    7.0000    0.2882
5.0000    6.0000    0.1456
5.0000    7.0000    0.3168
6.0000    7.0000    0.2767

I want to sort rows according to the elements of column 3 in a descending order. So that the sorted array is like:

1.0000 3.0000 0.8050

1.0000 4.0000 0.7116

2.0000 4.0000 0.6682

3.0000 6.0000 0.5989

.......................................

.......................................

Note that, if I use 'sortrows' then the array is sorted in a ascending order automatically which I could not change. If I use 'sort', each column is sorted individually which is not what I want. Any help would be highly appreciated.

-Faisal

3
How is this array stored? You could create a table out of it then using a data view you would be able to sort by any column you want... - G Berdal

3 Answers

3
votes

To sort in descending order you need a negative column index.

sortrows(a, -3)
2
votes

Read the next page of the manual:

sortrows(X,COL) sorts the matrix based on the columns specified in the vector COL.

especially the next sentence after the one I have quoted.

-1
votes

sortrows(yourArray,3)