Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #10308

Re: vector ordering problem

From Ray Koopman <koopman@sfu.ca>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: vector ordering problem
Date 2012-06-02 09:42 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jqcn5h$11s$1@smc.vnet.net> (permalink)
References <jq74k3$qkn$1@smc.vnet.net>

Show all headers | View raw


On May 30, 11:54 pm, Marco Lazzaretti <marco.lazzarett...@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}

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

vector ordering problem Marco Lazzaretti <marco.lazzaretti82@gmail.com> - 2012-05-31 06:54 +0000
  Re: vector ordering problem DC <b.gatessucks@gmail.com> - 2012-06-01 09:19 +0000
  Re: vector ordering problem Ray Koopman <koopman@sfu.ca> - 2012-06-02 09:42 +0000

csiph-web