1

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

1
  • 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... Commented Jul 26, 2010 at 12:00

3 Answers 3

3

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

sortrows(a, -3)
Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

Would vote up except why don't you just post "the next sentence".
@Dustin: because I grew tired of cutting and pasting text from the Matlab on-line help. I'm not proud of it, but it was all the strength I could muster at the time.
-1

sortrows(yourArray,3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.