MATLAB Answers

0

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

Asked by Tobias Schädel on 8 Jun 2018
Latest activity Commented on by Stephan
on 15 Jun 2018
c=[4,5,3;10,30,50;200,900,40]';
c=sortrows(c,1);
d=sortrows(c,3,'descend');

  1 Comment

"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.

Tags

Products


Release

R2015a

2 Answers

Answer by Stephan
on 8 Jun 2018
Edited by 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

Hi Tobias,
after answering you i saw that Steven Cobeldick gave the better answer - try like he suggested:
d = sortrows(c,[1 -3])
This should give you the result you expect and is much shorter. See Stevens answer below and accept it if you are happy now.
Best regards
Stephan
Thanks to all. The commends 'ascend' and 'descend' aren´t working in my version. But this is working.
d = sortrows(c,[1 -3])
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.


Answer by Stephen Cobeldick 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

  0 Comments

Sign in to comment.