MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Following position change in different vectors

  • To: mathgroup at smc.vnet.net
  • Subject: [mg117509] Re: Following position change in different vectors
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Mon, 21 Mar 2011 06:15:14 -0500 (EST)

On 3/20/11 at 4:53 AM, lt648 at hszk.bme.hu (Lengyel Tamas) wrote:

>1) I have 2 vectors with equal length. The positions of the first
>vector correspond to the second vector. So the first element of v2
>is calculated from the first element of v1, etc.

>2) I wish to sort the first vector in ascending magnitude using
>Sort[v1]. Naturally the positions of v1's elements change.
>
>What I want is the same change of position in v2's positions.

>(So e.g. if v1's sixth element moves after Sort to  the first
>position, I'd like v2's same position element to follow suit)

One way to do what you want would be to make use of Ordering as follows:

First, generate v1,v2 consistent with your description above

In[1]:= v1 = RandomReal[{-1, 1}, {5}];
v2 = v1^2;

In[3]:= p = Ordering[v1];
v1[[p]]
v2[[p]]

Out[4]= {-0.81929,-0.393001,-0.246831,0.237993,0.46827}

Out[5]= {0.671236,0.15445,0.0609256,0.0566405,0.219277}

Note, Out[4] is exactly the same as Sort[v1]

In[6]:= %%^2 == %

Out[6]= True

Or if you don't want to explicitly construct p

In[7]:= v2[[Ordering@v1]]
Sort[v1]

Out[7]= {0.671236,0.15445,0.0609256,0.0566405,0.219277}

Out[8]= {-0.81929,-0.393001,-0.246831,0.237993,0.46827}



  • Prev by Date: Re: Question about a CountorPlot3D
  • Next by Date: Re: Following position change in different vectors
  • Previous by thread: Re: Following position change in different vectors
  • Next by thread: Re: Following position change in different vectors