Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Find Position of many elements in a large list.
Replies: 6   Last Post: Aug 16, 2012 1:58 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Peter Pein

Posts: 1,147
Registered: 5/4/05
Re: Find Position of many elements in a large list.
Posted: Aug 15, 2012 3:33 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Am 14.08.2012 11:05, schrieb benp84@gmail.com:
> I have a sorted, 1-dimensional list X of 1,000,000 integers, and a sorted, 1-dimensional list Y of 10,000 integers. Most, but not all, of the elements of Y are also elements of X. I'd like to know the positions of the elements in X that are also in Y. What's the fastest way to compute this?
>
> I have an algorithm in mind but it requires lots of custom code and I'm wondering if there's a clever way to do it with built-in functions. Thanks.
>


Well, using the fact that the huge list (x) is sorted, I got a faster one.

binpos[xl_, y0_] :=
Block[{mid = BitShiftRight[Length[xl]], sel, pos},
If[mid === 0,
Boole[xl === {y0}],
If[xl[[mid]] <= y0, sel = Drop; pos = mid, sel = Take; pos = 0];
pos + binpos[sel[xl, mid], y0]
]];
Reap[Fold[Drop[#1, Sow[binpos[##]]] &, x, Intersection[x, y]]][[2,
1]] // Accumulate

needs only 20% of the time needed by
Position[x,Alternatives@@Intersection[x,y]]//Flatten

I'm sure, this can be slightly optimized.
Peter





Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.