Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #10287 > unrolled thread
| Started by | Marco Lazzaretti <marco.lazzaretti82@gmail.com> |
|---|---|
| First post | 2012-05-31 06:54 +0000 |
| Last post | 2012-06-02 09:42 +0000 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
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
| From | Marco Lazzaretti <marco.lazzaretti82@gmail.com> |
|---|---|
| Date | 2012-05-31 06:54 +0000 |
| Subject | vector ordering problem |
| Message-ID | <jq74k3$qkn$1@smc.vnet.net> |
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...
[toc] | [next] | [standalone]
| From | DC <b.gatessucks@gmail.com> |
|---|---|
| Date | 2012-06-01 09:19 +0000 |
| Message-ID | <jqa1eb$bi8$1@smc.vnet.net> |
| In reply to | #10287 |
Try (removed underscore from your name) : vector2[[positionvector1]]
[toc] | [prev] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2012-06-02 09:42 +0000 |
| Message-ID | <jqcn5h$11s$1@smc.vnet.net> |
| In reply to | #10287 |
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}
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web