Re: Find Position of many elements in a large list.

*To*: mathgroup at smc.vnet.net*Subject*: [mg127694] Re: Find Position of many elements in a large list.*From*: Bill Rowe <readnews at sbcglobal.net>*Date*: Wed, 15 Aug 2012 03:35:26 -0400 (EDT)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com*Delivered-to*: l-mathgroup@wolfram.com*Delivered-to*: mathgroup-newout@smc.vnet.net*Delivered-to*: mathgroup-newsend@smc.vnet.net

On 8/14/12 at 4:22 AM, benp84 at gmail.com wrote: >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. Here is potentially a fast way. In[14]:= x = Range[100]; y = Sort@RandomInteger[1000, 20]; First[y] Out[16]= 47 I executed this a couple of time to ensure at least one value in common In[17]:= common = Intersection[x, y]; Ordering[Ordering[Join[common, x]]][[;; Length@common]] - Range[0, Length@common - 1] Out18= {47,48,49} and to show this is right the common elements are: In[19]:= common Out[19]= {47,48,49} Ordering is a fairly fast Mathematica function. So, I think this will be reasonably fast.