MathGroup Archive 1995

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

Search the Archive

There must be a better way!

  • To: mathgroup at smc.vnet.net
  • Subject: [mg2322] There must be a better way!
  • From: pehowland at taz.dra.hmg.gb ()
  • Date: Tue, 24 Oct 1995 02:14:07 -0400
  • Organization: LSC2 Division, Defence Research Agency

Hi.  I'm trying to write a function that partitions a list into
segments of length given by another length. eg. 
	Given l1 = {A,B,C,D,E,F,G,H,I,J}
	and   l2 = {2,3,4,1}
I want to be able to say
	In[1] := MyPartition[l1, l2]
and get
	Out[1] = {{A,B}, {C,D,E}, {F,G,H,I}, {J}}

I've racked my brains, but have been unable to think of an elegant
solution, but instead managed the following Fortran-like function:

MyPartition[list1_, list2_] :=
Module[{ans,l},
	ans={};
	l = list1;
	Do	[
		AppendTo[ans, Take[l, list2[[i]]]];
		l = Drop[l, list2[[i]]],
		{i, Length[list2]}
		];
	ans
	]

Someone put me out of my misery!  How can I code this in a more efficient
manner?!

Paul E Howland                                  Tel. +44 (0)1684 895767
Long Range Ground Radar Sensors                 Fax. +44 (0)1684 896315
LSC2 Division                                 Email. PEHOWLAND at DRA.HMG.GB
Defence Research Agency
St Andrews Road
Malvern
Worcestershire, WR14 3PS, UK
=========================================================================


  • Prev by Date: Re: Problems with Mathmetica and Solaris2.4
  • Next by Date: Questions on data transferring and analysis
  • Previous by thread: RE: PC and MAC MMA compatibility
  • Next by thread: RE: There must be a better way!