MathGroup Archive 2006

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

Search the Archive

Re: List manipulation question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg67188] Re: List manipulation question
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Sun, 11 Jun 2006 23:07:49 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <e6ge0k$nh3$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

LectorZ wrote:
> Dear experts,
> 
> I need to calculate distances between the elements of the following
> list:
> 
> list={3700, 3800, 3900, 3950, 4000, 4050, 4100, 4150, 4200, 4250, 4300,
> 4350,
> 4400, 4450, 4500, 4550, 4600, 4650}
> 
> with the following logic:
> 
> a) If it is the first element, then take the simple difference to the
> next (i.e. 3800 - 3700)
> b) If it is the last element, then take the simple difference to the
> second last (i.e. 4650 -4600)
> c) the difference for all elements in between shoud be calculated as
> (next - last)/2 (i.e. (3900 - 3700)/2)
> 
> This list should come out as a result:
> 
> result={100,100,75,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50}
> 
> Thanks a lot for your help
> 
> LZ
> 
Hi Lector,

What about the following function?

In[1]:=
list = {3700, 3800, 3900, 3950, 4000, 4050, 4100,
     4150, 4200, 4250, 4300, 4350, 4400, 4450, 4500,
     4550, 4600, 4650};

In[2]:=
myDistance[x_List] :=
	Module[
		{lst},
		lst = Partition[x, 3, 1];
		Join[
			{First[lst][[2]] - First[lst][[1]]},
			((Last[#1] - First[#1])/2 & ) /@ lst,
			{Last[lst][[3]] - Last[lst][[2]]}
		]
	]

In[3]:=
myDistance[list]

Out[3]=
{100, 100, 75, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,

   50, 50, 50, 50, 50}

Best regards,
Jean-Marc


  • Prev by Date: Re: List manipulation question
  • Next by Date: Re: List manipulation question
  • Previous by thread: Re: List manipulation question
  • Next by thread: Re: List manipulation question