MathGroup Archive 2012

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

Search the Archive

Re: vector ordering problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg126713] Re: vector ordering problem
  • From: Ray Koopman <koopman at sfu.ca>
  • Date: Sat, 2 Jun 2012 05:41:28 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <jq74k3$qkn$1@smc.vnet.net>

On May 30, 11:54 pm, Marco Lazzaretti <marco.lazzarett... at gmail.com>
wrote:
> 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...

"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}



  • Prev by Date: Re: Stop on message?
  • Next by Date: Re: Memory Blowup Issues
  • Previous by thread: Re: vector ordering problem
  • Next by thread: PlotMarker in ContourPlot