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


Groups > comp.soft-sys.math.mathematica > #11377 > unrolled thread

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

Started byBill Rowe <readnews@sbcglobal.net>
First post2012-08-16 06:07 +0000
Last post2012-08-16 06:07 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Re: Find Position of many elements in a large list. Bill Rowe <readnews@sbcglobal.net> - 2012-08-16 06:07 +0000

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

FromBill Rowe <readnews@sbcglobal.net>
Date2012-08-16 06:07 +0000
SubjectRe: Find Position of many elements in a large list.
Message-ID<k0i2ma$1cj$1@smc.vnet.net>
On 8/15/12 at 3:31 AM, petsie@dordos.net (Peter Pein) wrote:

>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?

>the fastest way I was able to find is about four times faster than the
>naive approach

>Flatten[Position[x, Alternatives @@ Intersection[x, y], 1]]

>Dropping successively the irrelevant parts of the huge list x,
>searching becomes faster:

>Reap[
>Fold[ Drop[#1, Sow[LengthWhile[#1, Function[x0, x0 <= #2]]]] &, x,
>Intersection[x, y]
>]
>][[2, 1]] // Accumulate

On my system

In[1]:= x = Range[10^6];

In[2]:= y = RandomInteger[10^6, 5]

Out[2]= {94085,367746,149688,500015,542925}

In[3]:= Timing[
  Flatten[Position[x, Alternatives @@ Intersection[x, y], 1]]]

Out[3]= {0.277049,{94085,149688,367746,500015,542925}}

In[4]:= Timing[
  Reap[Fold[Drop[#1, Sow[LengthWhile[#1, Function[x0, x0 <=
#2]]]] &,
      x, Intersection[x, y]]][[2, 1]] // Accumulate]

Out[4]= {0.900927,{94085,149688,367746,500015,542925}}

In[5]:= Timing[
  Ordering[Ordering[Join[y, x]]][[;; Length@y]] -
   Range[0, Length@y - 1]]


Out[5]= {0.021082,{94085,367747,149687,500015,542925}}

[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web