MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How does Position work?


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


  • Prev by Date: Re: Using NDSolve and Finding a specific Value
  • Next by Date: Re: Re: silly problem
  • Previous by thread: How does Position work?
  • Next by thread: NDSolve: PDE's with more than 2 indep. variables.