MathGroup Archive 1998

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

Search the Archive

Re: Summing over like elements

  • To: mathgroup at smc.vnet.net
  • Subject: [mg13866] Re: Summing over like elements
  • From: "Allan Hayes" <hay at haystack.demon.cc.uk>
  • Date: Wed, 2 Sep 1998 01:31:00 -0400
  • References: <6sdb8i$peq@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Mitchell Kaplan wrote in message <6sdb8i$peq at smc.vnet.net>...
>I have a 2 column array.  Column 1 has some values, many of which are
>repeated.  Column 2 has probabilities of these values.
>
>I would like to sum the probabilities for each unique element in column
>1. For example:
>
>{{1, .5} , {2, .2} , {1, .1}}  should give me:
>
>{{1, .6} , {2, .2}}
>
>
>I'm sure I can do this with a "Do" loop -- however it seems that in
>general "Do's" are pretty innefficient.
>
>Does anyone have any suggestions?
>
>                Mitch
>
>


Mitch:

In[1]:=
collect2[x_]:={#[[1,1]],Plus@@#[[2]]}&/@
    Transpose/@Split[Sort[x], #1[[1]]== #2[[1]]&]

In[2]:=
tb2 = Table[Random[Integer,50],{10000},{2}];

In[3]:=
Timing[collect2[tb2]]
Out[3]=
{1.54
Second,{{0,4888},{1,4798},{2,5705},{3,4337},{4,4749},{5,4814},{6,4720},{
     
7,4423},{8,5298},{9,4112},{10,5006},{11,4571},{12,4066},{13,4108},{14,
     
5438},{15,4640},{16,4720},{17,4910},{18,5010},{19,5511},{20,5038},{21,
     
4571},{22,4254},{23,5977},{24,5029},{25,4611},{26,4663},{27,4914},{28,
     
4657},{29,4932},{30,4767},{31,4893},{32,4819},{33,5042},{34,4715},{35,
     
4774},{36,5345},{37,5173},{38,5581},{39,5641},{40,5180},{41,4677},{42,
     
4089},{43,4754},{44,5545},{45,4783},{46,5180},{47,4244},{48,5654},{49,
      5100},{50,4446}}}

------------------------------------------------------------- Allan
Hayes
Training and Consulting
Leicester UK
http://www.haystack.demon.co.uk
hay at haystack.demon.co.uk
voice: +44 (0)116 271 4198
fax: +44(0)116 271 8642



  • Prev by Date: Sound in QuickTime
  • Next by Date: problem with Series
  • Previous by thread: Re: Summing over like elements
  • Next by thread: Re: Summing over like elements