MathGroup Archive 2007

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

Search the Archive

Re: Segregating the elements of a list based on given lower and upper bounds

  • To: mathgroup at smc.vnet.net
  • Subject: [mg77227] Re: Segregating the elements of a list based on given lower and upper bounds
  • From: Adriano Pascoletti <adriano.pascoletti at gmail.com>
  • Date: Wed, 6 Jun 2007 06:44:09 -0400 (EDT)
  • References: <f43hin$342$1@smc.vnet.net>

On Jun 5, 1:31 pm, "R.G" <gobiitha... at yahoo.com.my> 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.
> {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

This gives the elements of A falling in the intervals B

In[3]:=(Cases[A, x_ /; #1[[1]] <= x <= #1[[2]]] & ) /@ B
Out[3]={{6.32553}, {8.56784}, {}, {14.8876}, {16.1871, 17.2285}}

The elements and their number are given by

In[4]:=(({#1, Length[#1]} & )[Cases[A,
     x_ /; #1[[1]] <= x <= #1[[2]]]] & ) /@ B

Out[4]={{{6.32553}, 1}, {{8.56784}, 1}, {{}, 0}, {{14.8876}, 1},
  {{16.1871, 17.2285}, 2}}

If you are interested in consecutive intervals
B = {{0, 7}, {7, 10}, {10, 12}, {12, 15}, {15, 18}};
and have Mathematica 6 you can use BinLists

In[11]:= BinLists[A, {Union[Flatten[B]]}]
Out[11]= {{6.32553}, {7.09956, 8.56784, 7.40711}, {10.0834},
{14.8876},  {16.1871, 15.3989, 17.2285}}

In[12]:= ({#1, Length[#1]} & ) /@ BinLists[A, {Union[Flatten[B]]}]
Out[12]= {{{6.32553}, 1}, {{7.09956, 8.56784, 7.40711},   3},
{{10.0834}, 1},  {{14.8876}, 1}, {{16.1871, 15.3989, 17.2285}, 3}

Adriano Pascoletti



  • Prev by Date: Re: Iterate through a list help
  • Next by Date: Re: NDSolve, Do loop, and Plot
  • Previous by thread: Re: Segregating the elements of a list based on given lower and upper bounds
  • Next by thread: Re: Segregating the elements of a list based on given lower and upper bounds