Re: Re: modified list interval calculation
- To: mathgroup at smc.vnet.net
- Subject: [mg21149] Re: [mg21058] Re: modified list interval calculation
- From: "Tomas Garza" <tgarza at mail.internet.com.mx>
- Date: Fri, 17 Dec 1999 01:21:58 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Deborah Leddon[Dleddon at students.cas.unt.edu] wrote: > I am having trouble with the problem below: > > I am trying to remove from list, t, those numbers in list, nt, that > match and then I want to calculate the differences (intervals ) > between succesive numbers in list, t, that do not contain the > removed number. For example: > the two lists are; > t = {1,4,4.5,6,7,8,8.9,9,10,12,140,16,16.8,17,18, > 19,19.1,19.3,19.5,20} > nt = {8,8.9,10,17} > > the resulting intervals would look like: > 4 -1, 4.5 - 4, 6 - 4.5, 7 - 6, 140 -12, 16 - 140, 16.8 - 16, 19 - 18, > 19.1 -19, 19.3 - 19.1, 19.5 - 19.3, 20 - 19.5 > and of course the final list would be: > intervals = {3, .5, 1.5, 1, 128, -124, .8, 1, .1, .2, .2, .5}; > > Note that 9 was omitted from the calculation of intervals because it > would have been involved in a calculation with a removed number > from the list, t. Probably this could be improved, but anyway here it goes: In[1]:= t = {1, 4, 4.5, 6, 7, 8, 8.9, 9, 10, 12, 140, 16, 16.8, 17, 18, 19, 19.1, 19.3, 19.5, 20}; nt = {8, 8.9, 10, 17}; In[3]:= ps = Position[t, x_ /; MemberQ[nt, x]] Out[3]= {{6}, {7}, {9}, {14}} In[4]:= c = ReplacePart[t, "*", ps] Out[4]= {1, 4, 4.5, 6, 7, "*", "*", 9, "*", 12, 140, 16, 16.8, "*", 18, 19, 19.1, \ 19.3, 19.5, 20} In[5]:= cr = Rest[c] Out[5]= {4, 4.5, 6, 7, "*", "*", 9, "*", 12, 140, 16, 16.8, "*", 18, 19, 19.1, 19.3, \ 19.5, 20} In[6]:= br = Drop[c, -1] Out[6]= {1, 4, 4.5, 6, 7, "*", "*", 9, "*", 12, 140, 16, 16.8, "*", 18, 19, 19.1, \ 19.3, 19.5} The intervals you want are given by In[7]:= Cases[Transpose[{br, cr}], {x_?NumberQ, y_?NumberQ}] Out[7]= {{1, 4}, {4, 4.5}, {4.5, 6}, {6, 7}, {12, 140}, {140, 16}, {16, 16.8}, {18, 19}, {19, 19.1}, {19.1, 19.3}, {19.3, 19.5}, {19.5, 20}} and the final list is In[8]:= intervals = % /. {x_, y_} -> y - x Out[8]= {3, 0.5, 1.5, 1, 128, -124, 0.8, 1, 0.1, 0.2, 0.2, 0.5} Tomas Garza Mexico City