Re: There must be a better way!
- To: mathgroup at smc.vnet.net
- Subject: [mg2387] Re: [mg2322] There must be a better way!
- From: Count Dracula <lk3a at kelvin.seas.virginia.edu>
- Date: Tue, 31 Oct 1995 23:23:09 -0500
- Organization: University of Virginia
In Article: 1609 of comp.soft-sys.math.mathematica
Allan Hayes <hay at haystack.demon.co.uk> wrote:
> Paul Howland <pehowland at taz.dra.hmg.gb>
> Subject: Re: [mg2322] There must be a better way!
> Asks about "repeated takes": for example given
> {A,B,C,D,E,F,G,H,I,J} and {2,3,4,1}
> he wants to get
> {{A,B}, {C,D,E}, {F,G,H,I}, {J}}.
> Here is one way:
> TakeRepeated[l_, t_]:=
> First/@Rest[
> FoldList[Through[{Take,Drop}[#1[[2]],#2]]&, {{},l}, t]
> ]
Here's another way:
take[list_, index_] :=
With[{inx = FoldList[Plus, First[index], Rest[index]]},
Take[list, #]& /@ Transpose[{inx - index + 1, inx}]
]
Here's a timing comparison I ran
list = Range[8000]
index = Range[2,126]
runt := Timing[take[list, index]][[1]]
runT := Timing[TakeRepeated[list, index]][[1]]
check := take[list, index] == TakeRepeated[list, index]
In[5]:= runt
Out[5]= 0.1 Second
In[6]:= runT
Out[6]= 1.54 Second
In[7]:= check
Out[7]= True
--
___________________________________________________________________________________
Levent Kitis lk3a at cars.mech.virginia.edu lk3a at kelvin.seas.virginia.edu
University of Virginia Department of Mechanical, Aerospace, and Nuclear Engineering