RE: Count pattern
- To: mathgroup at smc.vnet.net
- Subject: [mg41302] RE: [mg41289] Count pattern
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Wed, 14 May 2003 08:08:46 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: Søren Merser [mailto:merser at image.dk] To: mathgroup at smc.vnet.net >Sent: Tuesday, May 13, 2003 10:20 AM >To: mathgroup at smc.vnet.net >Subject: [mg41302] [mg41289] Count pattern > > > >hi there > >i need a litle help on a problem that has puzzeled me for some time > >i want to count the number of elements in s with value greater >or equal >to each element in t > >t = {1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 17, 27, 28, 30, 36, 50} > >s = {1, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 8, 10, 12, 12, 14, 17, 20, 27, >27, 28, 30, 30, 36, 38, 40, 45, 50, 50, 50, 63, 132, 132} > >the result should be: {33, 30, 28, 27, 26, 25, 24, 23, 21, 20, 18, 17, >15, 13, 12, 10, 6} > > > >kind regards soren > > Søren, two ideas: (1) use Count: In[4]:= res = Function[tx, Count[s, sx_ /; sx >= tx]] /@ t Out[4]= {33, 30, 28, 27, 26, 25, 24, 23, 21, 20, 18, 17, 15, 13, 12, 10, 6} or (2) construct an InterpolatingFunction: In[9]:= d = {#1, Length[s] + 1 - #2} & @@@ Prepend[Union[Transpose[{Sort[s], Range[Length[s]]}], SameTest -> (First[#1] === First[#2] &)], {0, 0}] Out[9]= {{0, 34}, {1, 33}, {2, 30}, {3, 28}, {4, 27}, {5, 26}, {6, 25}, {7, 24}, {8, 23}, {10, 21}, {12, 20}, {14, 18}, {17, 17}, {20, 16}, {27, 15}, {28, 13}, {30, 12}, {36, 10}, {38, 9}, {40, 8}, {45, 7}, {50, 6}, {63, 3}, {132, 2}} In[11]:= Interpolation[d, InterpolationOrder -> 0] /@ t Out[11]= {33, 30, 28, 27, 26, 25, 24, 23, 21, 20, 18, 17, 15, 13, 12, 10, 6} This for GreaterOrEqual, you must however be cautious constructing the InterpolationFunction in case of Greater (then don't use Union). See e.g. this alternative way: In[23]:= d = First /@ Split[Transpose[{Prepend[Sort[s], Min[s] - 1], Range[Length[s] + 1, 1, -1]}], (First[#1] === First[#2] &)] Out[23]= {{0, 34}, {1, 33}, {2, 30}, {3, 28}, {4, 27}, {5, 26}, {6, 25}, {7, 24}, {8, 23}, {10, 21}, {12, 20}, {14, 18}, {17, 17}, {20, 16}, {27, 15}, {28, 13}, {30, 12}, {36, 10}, {38, 9}, {40, 8}, {45, 7}, {50, 6}, {63, 3}, {132, 2}} -- Hartmut Wolf