Thread Subject: Sorting Two Vectors In Terms of One

Subject: Sorting Two Vectors In Terms of One

From: Ali Moradi

Date: 8 Oct, 2009 19:13:02

Message: 1 of 8

Hi,

I have two corresponding vectors of equal size, X & Y. I want to sort both of them in ascending order in terms of X. So X(1) has a corresponding value Y(1), and I want to sort them in ascending order, but still keep them teamed up with each other...I'm not sure if I'm making any sense, but hopefully you guys understand what I'm trying to do.

My ultimate goal is that I'm trying to plot x vs y, but since those X's aren't in ascending order, they are looping back on themselves, and I want to sort them out so that way I will get a normal graph.

If anyone has any suggestions, I'd really appreciate it!

- Ali

Subject: Sorting Two Vectors In Terms of One

From: dpb

Date: 8 Oct, 2009 19:45:56

Message: 2 of 8

Ali Moradi wrote:
> Hi,
>
> I have two corresponding vectors of equal size, X & Y. I want to sort
> both of them in ascending order in terms of X. So X(1) has a
> corresponding value Y(1), and I want to sort them in ascending order,
> but still keep them teamed up with each other...I'm not sure if I'm
> making any sense, but hopefully you guys understand what I'm trying
> to do.

doc sort % especially optional return paramter

--

Subject: Sorting Two Vectors In Terms of One

From: Jan Simon

Date: 8 Oct, 2009 20:03:03

Message: 3 of 8

Dear Ali Moradi!

> I have two corresponding vectors of equal size, X & Y. I want to sort both of them in ascending order in terms of X. So X(1) has a corresponding value Y(1), and I want to sort them in ascending order, but still keep them teamed up with each other...I'm not sure if I'm making any sense, but hopefully you guys understand what I'm trying to do.
>
> My ultimate goal is that I'm trying to plot x vs y, but since those X's aren't in ascending order, they are looping back on themselves, and I want to sort them out so that way I will get a normal graph.

Of course you are making any sense!
Your description makes some sense also. Try:

  X = randperm(10);
  y = rand(1, 10);
  [Xsorted, SortIndex] = sort(X);
  Ysorted = Y(SortIndex);

  plot(X, Y, 'ro');
  hold('on');
  plot(Xsorted, Ysorted, 'b-');

Does this hit your point?
Kind regards, Jan

Subject: Sorting Two Vectors In Terms of One

From: Ali Moradi

Date: 8 Oct, 2009 20:18:01

Message: 4 of 8

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <halghn$gu2$1@fred.mathworks.com>...
> Dear Ali Moradi!
>
> > I have two corresponding vectors of equal size, X & Y. I want to sort both of them in ascending order in terms of X. So X(1) has a corresponding value Y(1), and I want to sort them in ascending order, but still keep them teamed up with each other...I'm not sure if I'm making any sense, but hopefully you guys understand what I'm trying to do.
> >
> > My ultimate goal is that I'm trying to plot x vs y, but since those X's aren't in ascending order, they are looping back on themselves, and I want to sort them out so that way I will get a normal graph.
>
> Of course you are making any sense!
> Your description makes some sense also. Try:
>
> X = randperm(10);
> y = rand(1, 10);
> [Xsorted, SortIndex] = sort(X);
> Ysorted = Y(SortIndex);
>
> plot(X, Y, 'ro');
> hold('on');
> plot(Xsorted, Ysorted, 'b-');
>
> Does this hit your point?
> Kind regards, Jan


You're amazing! It worked! By the way, can you give me a quick explanation on what each line does so I learn it for the future? I'm very new to MatLab, and I'm still getting the hang of things. Thanks again for your help!

- Ali

Subject: Sorting Two Vectors In Terms of One

From: Jan Simon

Date: 8 Oct, 2009 20:35:18

Message: 5 of 8

Dear Ali!

> By the way, can you give me a quick explanation on what each line does so I learn it for the future? I'm very new to MatLab, and I'm still getting the hang of things.

> > X = randperm(10);
A random permutation of the numbers 1 to 10.

> > y = rand(1, 10);
A random vector of size [1 x 10].

> > [Xsorted, SortIndex] = sort(X);
Sort the xector X in ascending order and reply the sorting index as 2nd output.

> > Ysorted = Y(SortIndex);
Sort Y in the same order of Xsorted.

> > plot(X, Y, 'ro');
Plot Y versus X, 'r' means 'red', 'o' means a circle.

> > hold('on');
Do not delete the first plot if the 2nd is drawn.

> > plot(Xsorted, Ysorted, 'b-');
Plot sorted values, 'b' is 'blue', '-' means a connected line.

See also:
  help sort
  help plot
  help randperm
  help rand

Welcome at Matlab.
By the way: There are differences between Matlab, MATLAB and MatLab. Depending on whom you are talking with, these differences are essential or they concern just the upper/lower case of the charcters. ;-)

Kind regadrs, Jan

Subject: Sorting Two Vectors In Terms of One

From: dpb

Date: 8 Oct, 2009 20:34:48

Message: 6 of 8

Ali Moradi wrote:
...

> You're amazing! It worked! By the way, can you give me a quick
> explanation on what each line does so I learn it for the future? I'm
> very new to MatLab, and I'm still getting the hang of things. ...

Then you need to also learn to learn... :)

try

doc randperm

etc., ...

and follow along.

Note in

the call to sort() the two returned values and see how that was used and
how my suggestion points you that way...

--

Subject: Sorting Two Vectors In Terms of One

From: Ali Moradi

Date: 9 Oct, 2009 02:44:03

Message: 7 of 8

dpb - Sorry, I normally do check the documents and help files, but this time I couldn't really figure out from the doc files how I could sort the vectors like that. I normally don't try to post until I've at least checked the help files and done a basic google search. =)


Jan - When I did what you said, I actually removed the randperm and rand functions because they were making it do some weird things. I just put this part in:

[dthetasorted, SortIndex] = sort(dtheta);
Tsorted = T(SortIndex);

dtheta and T being the names of my vectors. It seemed to work that way. Is it absolutely necessary for me to leave the randperm functions in there?

Thanks again for your help guys.

- Ali

Subject: Sorting Two Vectors In Terms of One

From: Darren Rowland

Date: 9 Oct, 2009 03:18:03

Message: 8 of 8

>
> [dthetasorted, SortIndex] = sort(dtheta);
> Tsorted = T(SortIndex);
>
> dtheta and T being the names of my vectors. It seemed to work that way. Is it absolutely necessary for me to leave the randperm functions in there?


The randperm function was used only to create some example data. Your solution without randperm is fine as is.

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

Contact us at files@mathworks.com