Re: Extract values and multilpicities from list
- To: mathgroup at smc.vnet.net
- Subject: [mg64943] Re: Extract values and multilpicities from list
- From: Bill Rowe <readnewsciv at earthlink.net>
- Date: Wed, 8 Mar 2006 01:00:20 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
On 3/7/06 at 6:11 AM, carlw at wolfram.com (Carl K. Woll) wrote:
>For the special case where the integers are positive and not too
>large, then the following approach is probably close to the
>fastest.
>runs[d_] :=
>SparseArray[Transpose[{d, Range[Length[d]]}] -> Table[1,
>{Length[d]}]] /. SparseArray[_, _, _, {_, {p_, __}, _}] :>
>Transpose[{Range[Length[p] - 1], Rest[p] - Most[p]}]
>The simplest solution is probably:
>runs2[d_] := {First[#], Length[#]} & /@ Split[Sort[d]]
>The output of runs will include integers with 0 length runs, so we
>need to eliminate them when comparing results:
>In[22]:= r1=runs[data];//Timing r2=runs2[data];//Timing
>DeleteCases[r1,{_,0}]===r2
>Out[22]= {0.485 Second,Null}
>Out[23]= {0.781 Second,Null}
>Out[24]= True
What can be accomplished using SparseArray seems to me quite amazing. But in this case (on my machine at least) the simpler runs2 executes faster. That is
In[69]:=
RandomSeed[0];
n=100000;
test=Table[Random[Integer,{1,1000}],{n}];
Timing[r1=runs[test];]
Timing[r2=runs2[test];]
Out[72]=
{0.08321 Second,Null}
Out[73]=
{0.072016 Second,Null}
--
To reply via email subtract one hundred and four