Re: Find Position of many elements in a large list.
- To: mathgroup at smc.vnet.net
- Subject: [mg127697] Re: Find Position of many elements in a large list.
- From: Bob Hanlon <hanlonr357 at gmail.com>
- Date: Wed, 15 Aug 2012 03:36: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
- References: <20120814082221.E17D66791@smc.vnet.net>
You did not specify what form of output was desired. X = Sort[RandomInteger[{0, 50}, 100]]; Y = Union[RandomInteger[{0, 50}, 10]]; Just positions in X of integers common to X and Y. Flatten[Position[X, #] & /@ Intersection[X, Y]] {1, 27, 28, 29, 34, 47, 48, 51, 52, 71, 72, 73, 74, 93, 94, 95, 96} Integers common to X and Y and their positions in X. {#, Flatten[Position[X, #]]} & /@ Intersection[X, Y] {{0, {1}}, {17, {27, 28}}, {18, {29}}, {22, {34}}, {28, {47, 48}}, {30, {51}}, {31, {52}}, {40, {71, 72, 73, 74}}, {47, {93, 94, 95, 96}}} Integers common to X and Y and their position ranges in X. {#, Flatten[Position[X, #]][[{1, -1}]]} & /@ Intersection[X, Y] {{0, {1, 1}}, {17, {27, 28}}, {18, {29, 29}}, {22, {34, 34}}, {28, {47, 48}}, {30, {51, 51}}, {31, {52, 52}}, {40, {71, 74}}, {47, {93, 96}}} Bob Hanlon On Tue, Aug 14, 2012 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. Thanks. >
- References:
- Find Position of many elements in a large list.
- From: benp84@gmail.com
- Find Position of many elements in a large list.