Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Sorting matrix in Ascending Order

Subject: Sorting matrix in Ascending Order

From: Ali Hussain

Date: 02 Jul, 2008 13:28:01

Message: 1 of 9

Hi,
I am trying to sort out two large matrix x & y. The values
are scattered. I am trying to sort out the matrix 'x' in
ascending order and also want to change the y cells
according to the corresponding 'y' cells. I have used the
sort function but dont know how to track which cells have
been changed.
 

Below Is part of the matrix

x=[11.3867
   11.4481
   11.7062
   11.7676
   12.0256
   12.0912
   12.3451
   12.4106
   12.6646
   12.7301
   12.9226
   12.9881
   13.0496
   13.2421
   13.3076
   13.3690
   13.5616
   13.6271
   11.3867
   11.4481
   11.7062
   11.7676
   12.0256
   12.0912
   12.3451
   12.4106
   12.6646
   12.7301
   12.9226
   12.9881]
y=[0.3175
0.3375
0.4675
0.5075
0.7125
0.7575
1.0075
1.0175
1.0625
1.0825
1.0975
1.0875
1.0675
0.9875
0.9725
0.9375
0.7775
0.7625
0.3175
0.3375
0.4675
0.5075
0.7125
0.7575
1.0075
1.0175
1.0625
1.0825
1.0975
1.0875]

Thanks

Subject: Re: Sorting matrix in Ascending Order

From: Steven Lord

Date: 02 Jul, 2008 13:32:51

Message: 2 of 9


"Ali Hussain" <hussaizj@aston.ac.uk> wrote in message
news:g4fvp1$os4$1@fred.mathworks.com...
> Hi,
> I am trying to sort out two large matrix x & y. The values
> are scattered. I am trying to sort out the matrix 'x' in
> ascending order and also want to change the y cells
> according to the corresponding 'y' cells. I have used the
> sort function but dont know how to track which cells have
> been changed.

*snip*

You're using the 1-output syntax for SORT. There's another syntax listed in
the help that you can use for SORT -- do you see how you can use the
information you obtain from that syntax to solve your question?

--
Steve Lord
slord@mathworks.com


Subject: Re: Sorting matrix in Ascending Order

From: Ali Hussain

Date: 02 Jul, 2008 13:46:01

Message: 3 of 9

"Steven Lord" <slord@mathworks.com> wrote in message <g4g023
$t79$1@fred.mathworks.com>...
>
> "Ali Hussain" <hussaizj@aston.ac.uk> wrote in message
> news:g4fvp1$os4$1@fred.mathworks.com...
> > Hi,
> > I am trying to sort out two large matrix x & y. The
values
> > are scattered. I am trying to sort out the matrix 'x'
in
> > ascending order and also want to change the y cells
> > according to the corresponding 'y' cells. I have used
the
> > sort function but dont know how to track which cells
have
> > been changed.
>
> *snip*
>
> You're using the 1-output syntax for SORT. There's
another syntax listed in
> the help that you can use for SORT -- do you see how you
can use the
> information you obtain from that syntax to solve your
question?
>
> --
> Steve Lord
> slord@mathworks.com
>

I dont understand what your refering to. Could you be a
bit more specific about it. I can sort the matrix, but
need some help on changing matrix 'y' accordingly.
Thanks

Subject: Re: Sorting matrix in Ascending Order

From: aasim Azooz

Date: 02 Jul, 2008 13:57:01

Message: 4 of 9

"Ali Hussain" <hussaizj@aston.ac.uk> wrote in message
<g4fvp1$os4$1@fred.mathworks.com>...
> Hi,
> I am trying to sort out two large matrix x & y. The
values
> are scattered. I am trying to sort out the matrix 'x' in
> ascending order and also want to change the y cells
> according to the corresponding 'y' cells. I have used
the
> sort function but dont know how to track which cells have
> been changed.
>
>
> Below Is part of the matrix
>
> x=[11.3867
> 11.4481
> 11.7062
> 11.7676
> 12.0256
> 12.0912
> 12.3451
> 12.4106
> 12.6646
> 12.7301
> 12.9226
> 12.9881
> 13.0496
> 13.2421
> 13.3076
> 13.3690
> 13.5616
> 13.6271
> 11.3867
> 11.4481
> 11.7062
> 11.7676
> 12.0256
> 12.0912
> 12.3451
> 12.4106
> 12.6646
> 12.7301
> 12.9226
> 12.9881]
> y=[0.3175
> 0.3375
> 0.4675
> 0.5075
> 0.7125
> 0.7575
> 1.0075
> 1.0175
> 1.0625
> 1.0825
> 1.0975
> 1.0875
> 1.0675
> 0.9875
> 0.9725
> 0.9375
> 0.7775
> 0.7625
> 0.3175
> 0.3375
> 0.4675
> 0.5075
> 0.7125
> 0.7575
> 1.0075
> 1.0175
> 1.0625
> 1.0825
> 1.0975
> 1.0875]
>
> Thanks
> use the following:
z=[x;y];
zz=sortrows(z,1) % if you are sorting by x or
sortrows(z,2) %jf you are sorting by y
x=zz(:,1);
y=zz(:,2);

Subject: Re: Sorting matrix in Ascending Order

From: aasim Azooz

Date: 02 Jul, 2008 13:58:01

Message: 5 of 9

"Ali Hussain" <hussaizj@aston.ac.uk> wrote in message
<g4fvp1$os4$1@fred.mathworks.com>...
> Hi,
> I am trying to sort out two large matrix x & y. The
values
> are scattered. I am trying to sort out the matrix 'x' in
> ascending order and also want to change the y cells
> according to the corresponding 'y' cells. I have used
the
> sort function but dont know how to track which cells have
> been changed.
>
>
> Below Is part of the matrix
>
> x=[11.3867
> 11.4481
> 11.7062
> 11.7676
> 12.0256
> 12.0912
> 12.3451
> 12.4106
> 12.6646
> 12.7301
> 12.9226
> 12.9881
> 13.0496
> 13.2421
> 13.3076
> 13.3690
> 13.5616
> 13.6271
> 11.3867
> 11.4481
> 11.7062
> 11.7676
> 12.0256
> 12.0912
> 12.3451
> 12.4106
> 12.6646
> 12.7301
> 12.9226
> 12.9881]
> y=[0.3175
> 0.3375
> 0.4675
> 0.5075
> 0.7125
> 0.7575
> 1.0075
> 1.0175
> 1.0625
> 1.0825
> 1.0975
> 1.0875
> 1.0675
> 0.9875
> 0.9725
> 0.9375
> 0.7775
> 0.7625
> 0.3175
> 0.3375
> 0.4675
> 0.5075
> 0.7125
> 0.7575
> 1.0075
> 1.0175
> 1.0625
> 1.0825
> 1.0975
> 1.0875]
>
> Thanks
> use the following:
z=[x;y];
zz=sortrows(z,1) % if you are sorting by x or
sortrows(z,2) %jf you are sorting by y
x=zz(:,1);
y=zz(:,2);
 Aasim Azooz

Subject: Re: Sorting matrix in Ascending Order

From: Jos

Date: 02 Jul, 2008 14:09:02

Message: 6 of 9

"Ali Hussain" <hussaizj@aston.ac.uk> wrote in message
<g4g0qp$91j$1@fred.mathworks.com>...
> "Steven Lord" <slord@mathworks.com> wrote in message <g4g023
> $t79$1@fred.mathworks.com>...
> >
> > "Ali Hussain" <hussaizj@aston.ac.uk> wrote in message
> > news:g4fvp1$os4$1@fred.mathworks.com...
> > > Hi,
> > > I am trying to sort out two large matrix x & y. The
> values
> > > are scattered. I am trying to sort out the matrix 'x'
> in
> > > ascending order and also want to change the y cells
> > > according to the corresponding 'y' cells. I have used
> the
> > > sort function but dont know how to track which cells
> have
> > > been changed.
> >
> > *snip*
> >
> > You're using the 1-output syntax for SORT. There's
> another syntax listed in
> > the help that you can use for SORT -- do you see how you
> can use the
> > information you obtain from that syntax to solve your
> question?
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> >
>
> I dont understand what your refering to. Could you be a
> bit more specific about it. I can sort the matrix, but
> need some help on changing matrix 'y' accordingly.
> Thanks
>

Steve Lord is refering to the following.

[sortedX, IND] = sort(X) ;

Try to figure out what the numbers of IND mean. Read
carefully through the help of sort! What happens if you
apply them to another array?

Jos

Subject: Re: Sorting matrix in Ascending Order

From: Ali Hussain

Date: 02 Jul, 2008 14:35:02

Message: 7 of 9

"Jos " <DELjos@jasenDEL.nl> wrote in message
<g4g25u$rrt$1@fred.mathworks.com>...
> "Ali Hussain" <hussaizj@aston.ac.uk> wrote in message
> <g4g0qp$91j$1@fred.mathworks.com>...
> > "Steven Lord" <slord@mathworks.com> wrote in message
<g4g023
> > $t79$1@fred.mathworks.com>...
> > >
> > > "Ali Hussain" <hussaizj@aston.ac.uk> wrote in message
> > > news:g4fvp1$os4$1@fred.mathworks.com...
> > > > Hi,
> > > > I am trying to sort out two large matrix x & y.
The
> > values
> > > > are scattered. I am trying to sort out the
matrix 'x'
> > in
> > > > ascending order and also want to change the y cells
> > > > according to the corresponding 'y' cells. I have
used
> > the
> > > > sort function but dont know how to track which
cells
> > have
> > > > been changed.
> > >
> > > *snip*
> > >
> > > You're using the 1-output syntax for SORT. There's
> > another syntax listed in
> > > the help that you can use for SORT -- do you see how
you
> > can use the
> > > information you obtain from that syntax to solve your
> > question?
> > >
> > > --
> > > Steve Lord
> > > slord@mathworks.com
> > >
> >
> > I dont understand what your refering to. Could you be
a
> > bit more specific about it. I can sort the matrix,
but
> > need some help on changing matrix 'y' accordingly.
> > Thanks
> >
>
> Steve Lord is refering to the following.
>
> [sortedX, IND] = sort(X) ;
>
> Try to figure out what the numbers of IND mean. Read
> carefully through the help of sort! What happens if you
> apply them to another array?
>
> Jos

Thanks for the help. But the above code joins both matrix
and sorts them in ascending order.

I need to keep both x and y seperate and sort x in
ascending order and the cells that have been changed I want
to rearrange the cells accordingly.
Thanks

Subject: Re: Sorting matrix in Ascending Order

From: Jos

Date: 02 Jul, 2008 14:49:01

Message: 8 of 9

"Ali Hussain" <hussaizj@aston.ac.uk> wrote in message
<g4g3mm$imj$1@fred.mathworks.com>...
> "Jos " <DELjos@jasenDEL.nl> wrote in message
> <g4g25u$rrt$1@fred.mathworks.com>...
> > "Ali Hussain" <hussaizj@aston.ac.uk> wrote in message
> > <g4g0qp$91j$1@fred.mathworks.com>...
> > > "Steven Lord" <slord@mathworks.com> wrote in message
> <g4g023
> > > $t79$1@fred.mathworks.com>...
> > > >
> > > > "Ali Hussain" <hussaizj@aston.ac.uk> wrote in message
> > > > news:g4fvp1$os4$1@fred.mathworks.com...
> > > > > Hi,
> > > > > I am trying to sort out two large matrix x & y.
> The
> > > values
> > > > > are scattered. I am trying to sort out the
> matrix 'x'
> > > in
> > > > > ascending order and also want to change the y cells
> > > > > according to the corresponding 'y' cells. I have
> used
> > > the
> > > > > sort function but dont know how to track which
> cells
> > > have
> > > > > been changed.
> > > >
> > > > *snip*
> > > >
> > > > You're using the 1-output syntax for SORT. There's
> > > another syntax listed in
> > > > the help that you can use for SORT -- do you see how
> you
> > > can use the
> > > > information you obtain from that syntax to solve your
> > > question?
> > > >
> > > > --
> > > > Steve Lord
> > > > slord@mathworks.com
> > > >
> > >
> > > I dont understand what your refering to. Could you be
> a
> > > bit more specific about it. I can sort the matrix,
> but
> > > need some help on changing matrix 'y' accordingly.
> > > Thanks
> > >
> >
> > Steve Lord is refering to the following.
> >
> > [sortedX, IND] = sort(X) ;
> >
> > Try to figure out what the numbers of IND mean. Read
> > carefully through the help of sort! What happens if you
> > apply them to another array?
> >
> > Jos
>
> Thanks for the help. But the above code joins both matrix
> and sorts them in ascending order.
>
> I need to keep both x and y seperate and sort x in
> ascending order and the cells that have been changed I want
> to rearrange the cells accordingly.
> Thanks


??? sort(X) does not join x and y ...

OK, I think someone has to spell it out for you:

  [sx,i] = sort(x)
  y = y(i)

Jos

Subject: Re: Sorting matrix in Ascending Order

From: Ali Hussain

Date: 02 Jul, 2008 15:27:02

Message: 9 of 9

"Jos " <DELjos@jasenDEL.nl> wrote in message
<g4g4gt$spn$1@fred.mathworks.com>...
> "Ali Hussain" <hussaizj@aston.ac.uk> wrote in message
> <g4g3mm$imj$1@fred.mathworks.com>...
> > "Jos " <DELjos@jasenDEL.nl> wrote in message
> > <g4g25u$rrt$1@fred.mathworks.com>...
> > > "Ali Hussain" <hussaizj@aston.ac.uk> wrote in message
> > > <g4g0qp$91j$1@fred.mathworks.com>...
> > > > "Steven Lord" <slord@mathworks.com> wrote in
message
> > <g4g023
> > > > $t79$1@fred.mathworks.com>...
> > > > >
> > > > > "Ali Hussain" <hussaizj@aston.ac.uk> wrote in
message
> > > > > news:g4fvp1$os4$1@fred.mathworks.com...
> > > > > > Hi,
> > > > > > I am trying to sort out two large matrix x &
y.
> > The
> > > > values
> > > > > > are scattered. I am trying to sort out the
> > matrix 'x'
> > > > in
> > > > > > ascending order and also want to change the y
cells
> > > > > > according to the corresponding 'y' cells. I
have
> > used
> > > > the
> > > > > > sort function but dont know how to track which
> > cells
> > > > have
> > > > > > been changed.
> > > > >
> > > > > *snip*
> > > > >
> > > > > You're using the 1-output syntax for SORT.
There's
> > > > another syntax listed in
> > > > > the help that you can use for SORT -- do you see
how
> > you
> > > > can use the
> > > > > information you obtain from that syntax to solve
your
> > > > question?
> > > > >
> > > > > --
> > > > > Steve Lord
> > > > > slord@mathworks.com
> > > > >
> > > >
> > > > I dont understand what your refering to. Could you
be
> > a
> > > > bit more specific about it. I can sort the
matrix,
> > but
> > > > need some help on changing matrix 'y' accordingly.
> > > > Thanks
> > > >
> > >
> > > Steve Lord is refering to the following.
> > >
> > > [sortedX, IND] = sort(X) ;
> > >
> > > Try to figure out what the numbers of IND mean. Read
> > > carefully through the help of sort! What happens if
you
> > > apply them to another array?
> > >
> > > Jos
> >
> > Thanks for the help. But the above code joins both
matrix
> > and sorts them in ascending order.
> >
> > I need to keep both x and y seperate and sort x in
> > ascending order and the cells that have been changed I
want
> > to rearrange the cells accordingly.
> > Thanks
>
>
> ??? sort(X) does not join x and y ...
>
> OK, I think someone has to spell it out for you:
>
> [sx,i] = sort(x)
> y = y(i)
>
> Jos
>
Sorry Jos. I was talking about the code that was given by
Assim. I have managed to to do it and now just realised
you put the same code on here. Cheers Jos

Tags for this Thread

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.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics