MathGroup Archive 2005

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

Search the Archive

Re: A NewBie Question


Newbie answer:

In[47]:=
data = {a, b, c, d, e, f, g,
  h, i, j, k, l}
ind = {2, 3, 4}
Out[47]=
{a, b, c, d, e, f, g, h, i,
  j, k, l}
Out[48]=
{2, 3, 4}

In[85]:=
Table[{Take[data,
  {Sum[ind[[x]], {x, 1,
  y}] - ind[[y]] + 1,
  Sum[ind[[x]], {x, 1,
  y}]}], Complement[
  Take[data,
  {Sum[ind[[x]], {x, 1,
  y}] - ind[[y]] + 1,
  Length[data]}],
  Take[data,
  {Sum[ind[[x]], {x, 1,
  y}] - ind[[y]] + 1,
  Sum[ind[[x]], {x, 1,
  y}]}]]}, {y, 1, 3}][[
  All,1]]
Out[85]=
{{a, b}, {c, d, e},
  {f, g, h, i}}

János
P.S. I am sure there is a better way with Fold, I just do not feel Fold 
enough.
On Jan 19, 2005, at 1:59 AM, Zhe Hu wrote:



> Is there a way to take parts of a list as follows:
>
> takeParts[{a,b,c,d,e,f,g,h,i}, {2,3,4}]
>
> {{a,b}, {c,d,e}, {f,g,h,i}}
>
> A tedious way might be transforming {2, 3, 4} into {{1,2}, {3,5}, {6,
> 9}} (by Fold) and then Part/@ onto it. Is there a simpler way to do
> it?
>
> Thanks,
> Hu Zhe
>


  • Prev by Date: Re: compile / optimize
  • Next by Date: Re: random matrix from row and column sums
  • Previous by thread: Re: A NewBie Question
  • Next by thread: Re: A NewBie Question