开发者

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


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

sortrows(a, -3)


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.


sortrows(yourArray,3)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜