Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

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: [mg77304] Re: [mg77205] Segregating the elements of a list based on given lower and upper bounds
  • From: "R.G" <gobiithasan at yahoo.com.my>
  • Date: Wed, 6 Jun 2007 07:24:17 -0400 (EDT)

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ános <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}}}
> 
>  From 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ános
> 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 



  • Prev by Date: Re: pure function to generate a list of integrals
  • Next by Date: Re: Segregating the elements of a list based on given lower and upper bounds
  • Previous by thread: Re: Segregating the elements of a list based on given lower and upper bounds
  • Next by thread: Re: Re: Segregating the elements of a list based on given lower and upper bounds