Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
sortrows

Subject: sortrows

From: Ulrik Nash

Date: 28 Apr, 2010 15:34:04

Message: 1 of 4

Hi everyone,

I have a matrix, A, as follows:

3 1 2
1 3 1
4 5 2

Now I wish to sort A horizontally according to the top row, so that the largest value in the top row is in the top right corner. The final result should look like this:

1 2 3
3 1 1
5 2 4

How is this done?

Regards, Ulrik.

Subject: sortrows

From: someone

Date: 28 Apr, 2010 16:05:21

Message: 2 of 4

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hr9khc$74q$1@fred.mathworks.com>...
> Hi everyone,
>
> I have a matrix, A, as follows:
>
> 3 1 2
> 1 3 1
> 4 5 2
>
> Now I wish to sort A horizontally according to the top row, so that the largest value in the top row is in the top right corner. The final result should look like this:
>
> 1 2 3
> 3 1 1
> 5 2 4
>
> How is this done?
>
> Regards, Ulrik.

>> A

A =

     3 1 2
     1 3 1
     4 5 2

>> B = sortrows(A')'

B =

     1 2 3
     3 1 1
     5 2 4

Subject: sortrows

From: Roger Stafford

Date: 28 Apr, 2010 16:49:04

Message: 3 of 4

"someone" <someone@somewhere.net> wrote in message <hr9mc1$a4o$1@fred.mathworks.com>...
> ........
> >> B = sortrows(A')'
> ........

  It depends on what Ulrik wants to happen in case of ties in that first row. If he wants to continue to sort in other rows based on those "tied" columns, then that would be correct, but if the desire is to leave columns unchanged with ties in the first row, then he should use this:

 B = sortrows(A',1)';

  An example would be

 A = 2 2
     3 1
     2 5 .

Does he want

 B = 2 2
     1 3
     5 2

with sortrows(A')' or

 B = 2 2
     3 1
     2 5

with sortrows(A',1)' ?

Roger Stafford

Subject: sortrows

From: Ulrik Nash

Date: 29 Apr, 2010 08:29:08

Message: 4 of 4

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hr9ou0$15o$1@fred.mathworks.com>...
> "someone" <someone@somewhere.net> wrote in message <hr9mc1$a4o$1@fred.mathworks.com>...
> > ........
> > >> B = sortrows(A')'
> > ........
>
> It depends on what Ulrik wants to happen in case of ties in that first row. If he wants to continue to sort in other rows based on those "tied" columns, then that would be correct, but if the desire is to leave columns unchanged with ties in the first row, then he should use this:
>
> B = sortrows(A',1)';
>
> An example would be
>
> A = 2 2
> 3 1
> 2 5 .
>
> Does he want
>
> B = 2 2
> 1 3
> 5 2
>
> with sortrows(A')' or
>
> B = 2 2
> 3 1
> 2 5
>
> with sortrows(A',1)' ?
>
> Roger Stafford


Hi Roger,

That is correct:

B = sortrows(A',1)';

Thanks for the help both of you! Much appreciated.

Ulrik.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us