Re: Re: Conditional drop of list elements
- To: mathgroup at smc.vnet.net
- Subject: [mg63651] Re: Re: Conditional drop of list elements
- From: Paul <gleam at flashmail.com>
- Date: Mon, 9 Jan 2006 04:48:27 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Carl Woll wrote:
> The fastest approach for selecting a conditional
> subset of a large data
> set usually works as follows:
>
> 1. Construct a list of 0's and 1's corresponding to
> which elements you
> want to keep.
>
> 2. Extract the position of the 0's or 1's.
>
> 3. Use Part to obtain the subset.
>
> Here is a function that does the above:
>
> getsublist[data_] := Module[{mask, pos},
> mask = Sign[Abs[data[[All, 1]] - 1]];
> pos = SparseArray[mask, Automatic, 1] /.
> SparseArray[_, _, _, p_] :> Flatten[p[[2, 2]]];
> data[[pos]]]
>
> Some sample data:
>
> In[1]:=
> list1=Table[Random[Integer],{10^6},{5}];
>
> And a comparison:
>
> In[3]:=
> r1=Pick[list1,list1[[All,1]],1];//Timing
> r2=getsublist[list1];//Timing
> r1==r2
>
> Out[3]=
> {1.016 Second,Null}
>
> Out[4]=
> {0.187 Second,Null}
>
> Out[5]=
> True
>
> So, getsublist is over 5 times faster.
>
> Carl Woll
> Wolfram Research
Mr. Woll, I do so wish you would answer my "Puzzle Challenge" as I am sure you would present a wonderfully efficient method that I have never seen or thought of.
Paul