Re: Find Position of many elements in a large list.
- To: mathgroup at smc.vnet.net
- Subject: [mg127681] Re: Find Position of many elements in a large list.
- From: Peter Pein <petsie at dordos.net>
- Date: Wed, 15 Aug 2012 03:31:05 -0400 (EDT)
- Delivered-to: firstname.lastname@example.org
- Delivered-to: email@example.com
- Delivered-to: firstname.lastname@example.org
- Delivered-to: email@example.com
- References: <firstname.lastname@example.org>
Am 14.08.2012 11:05, schrieb benp84 at 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. > Hi, 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 I tried patterns and compilation; to no avail (see ). Cheers, Peter  https://dl.dropbox.com/u/3030567/Mathematica/getpositions.nb