Faisal
Faisal

Reputation: 23

need help with sorting an array

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

Upvotes: 1

Views: 182

Answers (3)

kennytm
kennytm

Reputation: 523184

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

sortrows(a, -3)

Upvotes: 3

nuriaion
nuriaion

Reputation: 2631

sortrows(yourArray,3)

Upvotes: -1

High Performance Mark
High Performance Mark

Reputation: 78316

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.

Upvotes: 2

Related Questions