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

• To: mathgroup at smc.vnet.net
• Subject: [mg77362] Re: [mg77304] Re: [mg77205] Segregating the elements of a list based on given lower and upper bounds
• From: János <janos.lobb at yale.edu>
• Date: Thu, 7 Jun 2007 03:54:05 -0400 (EDT)
• References: <200706061124.HAA00533@smc.vnet.net>

```I tried your code on my machine
In[15]:=
\$Version
Out[15]=
"5.2 for Mac OS X (64 bit) \
(June 20, 2005)"

but got only empty lists.

Here is another newbie approach - here I am goin' fishin' with the
buckets first - assuming lower case a and b for the sample you provided:
In[13]:=
Timing[Length /@
Table[Select[a,
Inequality[b[[i,1]],
LessEqual, #1, Less,
b[[i,2]]] & ],
{i, 1, Length[b]}]]
Out[13]=
{0.0006579999999996033*
Second, {1, 4, 0, 2, 4}}

Now for a Length[a]=10000:
In[24]:=
a = Table[Random[Real,
{0, 20}], {i, 1,
10000}];

In[31]:=
Timing[Length /@
Table[Select[a,
Inequality[b[[i,1]],
LessEqual, #1, Less,
b[[i,2]]] & ],
{i, 1, Length[b]}]]
Out[31]=
{0.3027660000000001*Second,
{3500, 1988, 502, 1530,
2480}}

Not too bad for a newbie :)

J=E1nos

On Jun 6, 2007, at 7:24 AM, R.G wrote:

> Hi, Thank you for your answer. Now lets change the lists as:
> A = {6.32553, 7.09956, 8.56784, 16.1871, 15.3989, 17.2285, 7.40711,
>    14.8876, 19.9068, 10.0834, 12.000};
> B = {{0, 7}, {7, 11}, {11, 12}, {12, 15}, {15, 20}};
> where the the first bound defined as 0<=i<7, it applies to the rest.
> I have written the following code for segregating the list:
>
> f[x_, y_] := Rest[FoldList[If[#2 >= x && #2 < y, Sow[#2], Null] &,
> 1,A]]
> Table[class[i] =
>    Delete[f[B[[i, 1]], B[[i, 2]]],
>     Position[f[B[[i, 1]], B[[i, 2]]], Null]], {i, 1, Length[B]}];
> Table[Length[class[i]], {i, 1, Length[B]}]
>
> It does the segregation as I wanted: {1, 4, 0, 2, 4}. However, if
> Length[A]=10,000, then it's quite slow. Is the code =
proper/efficient?
> Thanks, R.G.
>
>
> --- J=E1nos <janos.lobb at yale.edu> wrote:
>> Here is a newbie approach:
>> /Your bounds do not correspond to the distribution you desire :)/
>>
>> I use lower case a,b,c
>>
>> In[2]:=
>> sa = Sort[a]
>>
>> In[4]:=
>> c = Last[Reap[i = 1;
>>       While[i <= Length[sa],
>>        j = 1; While[j <=
>>            Length[b],
>>           If[b[[j,1]] <=
>>            sa[[i]] <= b[[j,
>>            2]], Sow[{b[[j]],
>>            sa[[i]]}, j]; j++,
>>            j++; Continue[]]; ]*
>>          i++; ]]]
>> Out[4]=
>> {{{{0, 7}, 6.32553}},
>>    {{{8, 10}, 8.56784}},
>>    {{{13, 15}, 14.8876}},
>>    {{{16, 18}, 16.1871},
>>     {{16, 18}, 17.2285}}}
>>
>>  =46rom there the distribution:
>>
>> In[22]:=
>> ({#1[[1,1]], Length[
>>       #1]} & ) /@ c
>> Out[22]=
>> {{{0, 7}, 1}, {{8, 10}, 1},
>>    {{13, 15}, 1}, {{16, 18},
>>     2}}
>>
>> J=E1nos
>> P.S  Those ranges where you do not have a value you can Union it in
>> with using Complement
>>
>>
>> --------------------------------
>> "I wish developing great products was as easy as writing a check. If
>>
>> that were the case, Microsoft would have some great products."
>> --Steve Jobs
>>
>>
>>
>>
>
>
> Send instant messages to your online friends http://
> uk.messenger.yahoo.com
>

---------------------------------------------
"Its like giving a glass of ice water to somebody in Hell"
Steve Jobs about iTunes on Windows

```

• Prev by Date: Re: Value of E
• Next by Date: Re: display change in version 6
• Previous by thread: Re: Segregating the elements of a list based on given lower and upper bounds
• Next by thread: Re: Hankel transformation // Fourier transformation for a circular function