Could you please tell me why the last part for the descending order isn´t working? Thank you!

1 view (last 30 days)
c=[4,5,3;10,30,50;200,900,40]';
c=sortrows(c,1);
d=sortrows(c,3,'descend');
  1 Comment
Stephen23
Stephen23 on 8 Jun 2018
Edited: Stephen23 on 8 Jun 2018
"Could you please tell me why the last part for the descending order isn´t working?"
What does "isn't working" mean? Are you getting an output that you do not expect? (if so what do you expect?) Or is MATLAB throwing an error? (if so, what is the complete error message, and what MATLAB version are you using?)

Sign in to comment.

Answers (2)

Stephan
Stephan on 8 Jun 2018
Edited: Stephan on 8 Jun 2018
Hi,
I will try to guess what you mean:
You want it sorted in ascending order after the first column and descending at the same time after the third column.
If we extend your numerical example a little, then it goes like this:
c=[4,5,3,4,3;10,30,50,40,10;200,900,40,1000,50]
gives
c =
4 10 200
5 30 900
3 50 40
4 40 1000
3 10 50
now:
d = sortrows(c,[1 3],{'ascend', 'descend'})
gives:
d =
3 10 50
3 50 40
4 40 1000
4 10 200
5 30 900
You wanted to do so?
Regardless of whether I have guessed right or wrong, what could be your problem:
Nevertheless, Stephen is absolutely right - The question is very unclear and gives no concrete statement of what your problem is. It is helpful for the volunteers here, if the questions are put as concrete as possible, error messages are attached and if necessary attachments are provided.
Best regards
Stephan
  5 Comments
Tobias Schädel
Tobias Schädel on 15 Jun 2018
Thanks to all. The commends 'ascend' and 'descend' aren´t working in my version. But this is working.
d = sortrows(c,[1 -3])
Stephan
Stephan on 15 Jun 2018
Hi Tobias,
fine. In order to help people with similar problems finding helpful answers, please accept S helpful answers.
Best regards
Stephan

Sign in to comment.


Stephen23
Stephen23 on 8 Jun 2018
Perhaps one of these does what you want:
>> c = [4,5,3;10,30,50;200,900,40]'
c =
4 10 200
5 30 900
3 50 40
>> sortrows(c,[1,-3])
ans =
3 50 40
4 10 200
5 30 900
>> sortrows(c,[-3,1])
ans =
5 30 900
4 10 200
3 50 40

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!