Re: Segregating the elements of a list based on given lower and upper
- To: mathgroup at smc.vnet.net
- Subject: [mg77281] Re: Segregating the elements of a list based on given lower and upper
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Wed, 6 Jun 2007 07:12:20 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <f43hin$342$1@smc.vnet.net>
R.G wrote: > Hi Mathgroup members, > > Say, I have a list with the following elements: > > A={6.32553, 7.09956, 8.56784, 16.1871, 15.3989, 17.2285, 7.40711, \ > 14.8876, 19.9068, 10.0834} > > and I have the following list with each {xvalue, yvalue}={lower bound, > upper bound}: > B={{0, 7}, {8, 10}, {11, 12}, {13, 15}, {16, 18}} > > How can I segregate values in A according to lower bound and upper > bound from B and find the number number of occurrence ?For example: > {0,7}={6.32553}, thus the number of occurrence is 1. > {8, 10}={7.09956,7.40711,8.56784}, the number of occurrence is 3. Here, I believe you meant the number of occurrences is 1 since only 8.56784 belongs to the interval. > {11,12}=None, the number of occurrence is 0. > > The code should be able work for any number Length[A] and Length[B]. > Any suggestion please? > Thank you, > R.G Assuming I have correctly understood what you want, you could use interval arithmetic as in the following expressions: In[1]:= lst = {6.32553, 7.09956, 8.56784, 16.1871, 15.3989, 17.2285, 7.40711, 14.8876, 19.9068, 10.0834}; bounds = {{0, 7}, {8, 10}, {11, 12}, {13, 15}, {16, 18}}; intervals = Interval /@ bounds; Total[Transpose[(Boole[IntervalMemberQ[#1, lst]] & ) /@ intervals]] Total[Transpose[Outer[Boole[IntervalMemberQ[#1, #2]] & , intervals, lst]]] Out[4]= {1, 1, 0, 1, 2} Out[5]= {1, 1, 0, 1, 2} Regards, Jean-Marc