Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1995

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

Search the Archive

Re: There must be a better way!

  • To: mathgroup at
  • Subject: [mg2387] Re: [mg2322] There must be a better way!
  • From: Count Dracula <lk3a at>
  • 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> wrote:

> Paul Howland <pehowland at>
> 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    lk3a at
University of Virginia Department of Mechanical, Aerospace, and Nuclear Engineering  

  • Prev by Date: More windowed mathematica shell?
  • Next by Date: Re: Special Nonlinear Fitting
  • Previous by thread: Re: More windowed mathematica shell?
  • Next by thread: Re: Special Nonlinear Fitting