Thread Subject: Sort columns of numbers

Subject: Sort columns of numbers

From: Phil

Date: 1 Sep, 2009 10:23:30

Message: 1 of 6

Hello,

This is undoubtably a simple question but it is a puzzling me. I have the following:

4 9
3 8
5 12
6 13
1 4
2 19

How can i sort the matrix so it reads:
1 4
2 19
3 8
4 9
5 12
6 13

i.e. how can i sort column 1 so that it is ascending but also move the corresponding value in column 2 to the correct position?

Any help would be great.

Thanks in advance,

Phil

Subject: Sort columns of numbers

From: us

Date: 1 Sep, 2009 10:29:02

Message: 2 of 6

"Phil " <harsh_classic@yahoo.co.uk> wrote in message <h7isn2$30i$1@fred.mathworks.com>...
> Hello,
>
> This is undoubtably a simple question but it is a puzzling me. I have the following:
>
> 4 9
> 3 8
> 5 12
> 6 13
> 1 4
> 2 19
>
> How can i sort the matrix so it reads:
> 1 4
> 2 19
> 3 8
> 4 9
> 5 12
> 6 13
>
> i.e. how can i sort column 1 so that it is ascending but also move the corresponding value in column 2 to the correct position?
>
> Any help would be great.
>
> Thanks in advance,
>
> Phil

a hint:

     help sortrows;

us

Subject: Sort columns of numbers

From: Nasser Abbasi

Date: 1 Sep, 2009 10:29:56

Message: 3 of 6


"Phil " <harsh_classic@yahoo.co.uk> wrote in message
news:h7isn2$30i$1@fred.mathworks.com...
> Hello,
>
> This is undoubtably a simple question but it is a puzzling me. I have the
> following:
>
> 4 9
> 3 8
> 5 12
> 6 13
> 1 4
> 2 19
>
> How can i sort the matrix so it reads:
> 1 4
> 2 19
> 3 8
> 4 9
> 5 12
> 6 13
>
> i.e. how can i sort column 1 so that it is ascending but also move the
> corresponding value in column 2 to the correct position?
>
> Any help would be great.
>
> Thanks in advance,
>
> Phil

EDU>> A=[4 9;
3 8;
5 12;
6 13;
1 4;
2 19];

sortrows(A)

ans =

     1 4
     2 19
     3 8
     4 9
     5 12
     6 13

--Nasser

Subject: Sort columns of numbers

From: Aurelien Queffurust

Date: 1 Sep, 2009 10:31:24

Message: 4 of 6

"Phil " <harsh_classic@yahoo.co.uk> wrote in message <h7isn2$30i$1@fred.mathworks.com>...
> Hello,
>
> This is undoubtably a simple question but it is a puzzling me. I have the following:
>
> 4 9
> 3 8
> 5 12
> 6 13
> 1 4
> 2 19
>
> How can i sort the matrix so it reads:
> 1 4
> 2 19
> 3 8
> 4 9
> 5 12
> 6 13
>
> i.e. how can i sort column 1 so that it is ascending but also move the corresponding value in column 2 to the correct position?
>
> Any help would be great.
>
> Thanks in advance,
>
> Phil

doc sortrows

Subject: Sort columns of numbers

From: Phil

Date: 1 Sep, 2009 10:39:03

Message: 5 of 6

Thanks to all!

"Aurelien Queffurust" <tug83@yahoo.fr> wrote in message <h7it5s$6l5$1@fred.mathworks.com>...
> "Phil " <harsh_classic@yahoo.co.uk> wrote in message <h7isn2$30i$1@fred.mathworks.com>...
> > Hello,
> >
> > This is undoubtably a simple question but it is a puzzling me. I have the following:
> >
> > 4 9
> > 3 8
> > 5 12
> > 6 13
> > 1 4
> > 2 19
> >
> > How can i sort the matrix so it reads:
> > 1 4
> > 2 19
> > 3 8
> > 4 9
> > 5 12
> > 6 13
> >
> > i.e. how can i sort column 1 so that it is ascending but also move the corresponding value in column 2 to the correct position?
> >
> > Any help would be great.
> >
> > Thanks in advance,
> >
> > Phil
>
> doc sortrows

Subject: Sort columns of numbers

From: Jan Simon

Date: 1 Sep, 2009 16:38:02

Message: 6 of 6

Dear Phil!

> 4 9
> 3 8
> 5 12
> 6 13
> 1 4
> 2 19
>
> How can i sort the matrix so it reads:
> 1 4
> 2 19
> 3 8
> 4 9
> 5 12
> 6 13
>
> i.e. how can i sort column 1 so that it is ascending but also move the corresponding value in column 2 to the correct position?

The already mentioned SORTROWS cares for the 2nd column also. If this is not wanted, you can sort the first column and apply the index to the 2nd column:
  A = [4, 9; ...
         3, 8; ...
         5, 12; ...
         6, 13; ...
         1, 4; ...
         2, 19];
  [A1, SortIndex] = sort(A(:, 1));
  ASorted = A(SortIndex, :);
or
  ASorted = [A1, A(SortIndex, 2)]; % slower

Th result differs from SORTROWS, if the numbers in the first column are not unique.

Kind regards, Jan

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
reference us 1 Sep, 2009 06:34:04
rssFeed for this Thread

Contact us at files@mathworks.com