Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #11354
| From | Peter Pein <petsie@dordos.net> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Find Position of many elements in a large list. |
| Date | 2012-08-15 07:33 +0000 |
| Message-ID | <k0fjcr$q85$1@smc.vnet.net> (permalink) |
| References | <k0d4bu$m8f$1@smc.vnet.net> |
| Organization | Time-Warner Telecom |
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
Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Find Position of many elements in a large list. benp84@gmail.com - 2012-08-14 09:05 +0000 Re: Find Position of many elements in a large list. Roland Franzius <roland.franzius@uos.de> - 2012-08-15 07:32 +0000 Re: Find Position of many elements in a large list. Peter Pein <petsie@dordos.net> - 2012-08-15 07:33 +0000 Re: Find Position of many elements in a large list. daniel.lichtblau0@gmail.com - 2012-08-15 07:34 +0000 Re: Find Position of many elements in a large list. Peter Pein <petsie@dordos.net> - 2012-08-15 07:39 +0000 Re: Find Position of many elements in a large list. daniel.lichtblau0@gmail.com - 2012-08-16 05:58 +0000
csiph-web