Re: Re: list manipulation
- To: mathgroup at smc.vnet.net
- Subject: [mg20626] Re: [mg20601] Re: list manipulation
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Thu, 4 Nov 1999 02:13:37 -0500
- References: <199911020735.CAA26984@smc.vnet.net.>
- Sender: owner-wri-mathgroup at wolfram.com
jim leddon wrote: > > Hi Folks, > > The program below takes the each value of the list,"events" , finds > which of these values falls within the intervals {x,y} which comprise > the list, "gaps" , then removes these values from the events list and > outputs this modified list which is called "eout". I'm getting an error > for incomplete expression, although I'm not sure if the algorithm itself > is doing the job. These reason why I wrote the loop to cue on the > integer parts of both lists is because these lists will eventually be > quite large, about 3000 elements each in which case I wanted to make the > process more efficient. > > Thanks if anyone can help. > Debbie L. > > gaps = {{1,5.5}, {2,4.3}, {2.7, 3.1}, {3.002, 4.007}, {10.001, > 10.007}, {10.101, 11.001}, {11.007, 12.0}}; > > events ={6.7, 8.9, 2.3, 2.789, 10, 11.002, 10.115, 3.02, 2.75}; > eout = {0}; > max = Length[events]; > Do[e = IntegerPart[events[[i]]; ei = events[[i]]; tmp1 = > Select[gaps, > > #[[1]] >= IntegerPart[e] && #[[2]] <= IntegerPart[e] &]; > For[ j=1, j<= Length[tmp1], j++, > If[ ei <= tmp1[[j,2]] && ei .= tmp1[[j,1]], > AppendTo[eout,ei]] > ], > {i,max} > ] > Delete[eout,1]; > eout The error is most likely related to bracket-matching. Presumably your first statement should be e = IntegerPart[events[[i]]] (* note third right-bracket *) There are several other issues that would lead one to believe this code was not carefully designed or tested. For example, your verbal description of how you would use integer parts does not correspond to what they actually do. Do you only want to check whether the integer part of an element in events lies in an interval in gaps? Or the element itself? Why do you extract integer parts only to explicitly take integer parts again? Do you expect to have 10.115 in eout (your method will not put it there). Also note the use of <= where you'd need >=, followed by a character pair .= that perhaps should be <=. Next, note that Delete[eout,1]; will do nothing to eout (because there is no assignment to eout), and, due to semi-colon, will also not output anything. All that said, here is some inefficient code that may do what you want. eout = {0}; max = Length[events]; Do[ e = IntegerPart[events[[i]]]; ei = events[[i]]; tmp1 = Select[gaps, #[[1]] <= IntegerPart[e] && #[[2]] >= IntegerPart[e] &]; For[ j=1, j<= Length[tmp1], j++, If[ ei <= tmp1[[j,2]] && ei >= tmp1[[j,1]], AppendTo[eout,ei]]], {i,max}] Delete[eout,1] For your example, it gives: Out[23]= {2.3, 2.3, 2.789, 2.789, 3.02, 3.02, 3.02, 2.75, 2.75} I am guessing that the multiple-counting was not desired. If so, one can remove it by taking Union. Also, as noted above, 10.115 does not appear on this list. I do not know if this was the intent or not; if not, the problem is in the way IntegerPart is used. I should also point out that there is no reason to initialize eout to {0} rather than {} (so you do not need to Delete the first element when finished), and there are good ways to avoid AppendTo (and it should be avoided because it will substantially slow the process for large lists). In this example you can use Interval and IntervalMemberQ to good advantage, and indeed the whole thing can be done with a simple Select, no loops or global variables are needed. In[26]:= igaps = Apply[Interval, gaps]; In[27]:= eout = Select[events, IntervalMemberQ[igaps,#]&] Out[27]= {2.3, 2.789, 10.115, 3.02, 2.75} To remove these from the original events, just do In[31]:= events = Complement[events, eout] Out[31]= {6.7, 8.9, 10, 11.002} Daniel Lichtblau Wolfram Research
- References:
- Re: list manipulation
- From: "jim leddon" <jleddon@home.com>
- Re: list manipulation