Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vector ordering problem

9 views
Skip to first unread message

Marco Lazzaretti

unread,
May 31, 2012, 2:54:59 AM5/31/12
to
Hi,
I have these two vectors, vector1 and vector2.
I need to have vector2 sorted by a positions vector, given by a
vector1.
e.g.
vector1 ={32,4,28,11,20}
position_vector1= Ordering[vector1]
position_vector1 = {5,1,4,2,3}

vector2 = {78,12,95,14,35}

requested output
{35,78,14,12,95}

that is, I have this position vector, as an output from another
calculation; then i need my input vector to be sorted as the position
vector says.
Obviously, my real input vector is quite bigger so i can't sort it
manually... I hope the small example can explain my problem better
than my words...

DC

unread,
Jun 1, 2012, 5:19:07 AM6/1/12
to
Try (removed underscore from your name) :

vector2[[positionvector1]]

Ray Koopman

unread,
Jun 2, 2012, 5:42:09 AM6/2/12
to
On May 30, 11:54 pm, Marco Lazzaretti <marco.lazzarett...@gmail.com>
wrote:
"Position vector" is an ambiguous term.
You need to distinguish between "to" lists and "from" lists.

The list of indices returned by Ordering is a "from" list:
the k'th element specifies where in the original list
the k'th element in the reordered list came *from*.

However, your position_vector1 seems to be a "to" list:
the k'th element specifies the position in the new list
that the k'th element in the old list should go *to*.

Ordering toggles between the two kinds of lists.

In[1]:= v1 = {32,4,28,11,20};

In[2]:= o1 = Ordering@v1
p1 = Ordering@o1
o1 === Ordering@p1

Out[2]= {2,4,5,3,1}
Out[3]= {5,1,4,2,3}
Out[4]= True

In[5]:= v2 = {78,12,95,14,35};

In[6]:= v2[[o1]]
v2[[p1]]

Out[6]= {12,14,35,95,78}
Out[7]= {35,78,14,12,95}

0 new messages