Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #10296 > unrolled thread
| Started by | Bob Hanlon <hanlonr357@gmail.com> |
|---|---|
| First post | 2012-06-01 09:18 +0000 |
| Last post | 2012-06-01 09:18 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.soft-sys.math.mathematica
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: vector ordering problem Bob Hanlon <hanlonr357@gmail.com> - 2012-06-01 09:18 +0000
| From | Bob Hanlon <hanlonr357@gmail.com> |
|---|---|
| Date | 2012-06-01 09:18 +0000 |
| Subject | Re: vector ordering problem |
| Message-ID | <jqa1ce$bhd$1@smc.vnet.net> |
In Mathematica you cannot use _ in names
vector1 = {32, 4, 28, 11, 20};
vector2 = {78, 12, 95, 14, 35};
positionVector1 = Ordering[vector1]
{2, 4, 5, 3, 1}
Note that this is not what you presented and
vector2[[positionVector1]]
{12, 14, 35, 95, 78}
A way to obtain your requested result
positionVector2 =
With[{v = Sort[vector1]}, Position[v, #] & /@ vector1 // Flatten]
{5, 1, 4, 2, 3}
vector2[[positionVector2]]
{35, 78, 14, 12, 95}
Bob Hanlon
On Thu, May 31, 2012 at 2:51 AM, Marco Lazzaretti
<marco.lazzaretti82@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...
>
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web