Re: Following position change in different vectors
- To: mathgroup at smc.vnet.net
- Subject: [mg117521] Re: Following position change in different vectors
- From: Peter Pein <petsie at dordos.net>
- Date: Mon, 21 Mar 2011 06:17:33 -0500 (EST)
- References: <im4iqj$d1b$1@smc.vnet.net>
Am 20.03.2011 10:53, schrieb Lengyel Tamas:
> Dear Mathworld Users,
>
> Sorry for asking your help again, I hope I'm not spamming too much.
> My problem is the following:
>
> 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)
>
> Thank you for your help in advance.
>
> Tamas
>
Hi Tamas,
use Ordering before sorting v1:
In[1]:= v1 = RandomSample[Range[5]];
v2 = v1^2
Out[2]= {1, 9, 4, 16, 25}
get v2 in the order of sorted v1:
In[3]:= v2s = v2[[Ordering[v1]]]
Out[3]= {1, 4, 9, 16, 25}
In[4]:= v1s = Sort@v1
Out[4]= {1, 2, 3, 4, 5}
Peter