Re: How does Position work?
- To: mathgroup at smc.vnet.net
- Subject: [mg85380] Re: [mg85313] How does Position work?
- From: DrMajorBob <drmajorbob at bigfoot.com>
- Date: Thu, 7 Feb 2008 22:31:28 -0500 (EST)
- References: <30638300.1202384648040.JavaMail.root@m08>
- Reply-to: drmajorbob at bigfoot.com
Your expression is a Map:
something /@ list
It does "something" to each element of "list", one at a time and returns
each result.
list = Riffle[ConstantArray[0, 10^4], ConstantArray[1, 10^4]];
Timing[Flatten[Position[list, #1]] & /@ list;]
{9.31428, Null}
There's NO REASON this should go faster, since you're doing an operation
20,000 times and assembling 20,000 results.
In your mind, perhaps, you're doing something different, as in:
Timing[Flatten[Position[list, #1]] & /@ Union@list;]
{0.022799, Null}
where Mathematica does "something" TWICE and assembles only TWO results.
Bobby
On Tue, 05 Feb 2008 18:45:44 -0600, P_ter <peter_van_summeren at yahoo.co.uk>
wrote:
> Hello,
> János wrote some time ago a solution which involved
> lstIn = {2, 3, 4, 4, 2, 1, 1, 5, 4}
> Flatten[Position[lstIn, #1]] & /@ lstIn
> This last line gives as result:
> {{1, 5}, {2}, {3, 4, 9}, {3, 4, 9}, {1, 5}, {6, 7}, {6, 7}, {8}, {3,
> 4, 9}}
> One can observe that the last 4 in lstIn has the same result as the
> first and second 4. Which should be.
> But! Did Position make a trie (for example) first and then give the
> results? The document about Position only tells that expressions are
> searched for in depth-first order. But that does not tell anything about
> the data structure.
> I used:
> lstIn1 = Riffle[ConstantArray[0, 10^4], ConstantArray[1, 10^4]];
> One should expect that this would work very fast, but it takes a long
> time.
> Does someone know about this?
> with friendly greetings,
> P_ter
>
>
--
DrMajorBob at bigfoot.com