Re: Summing over like elements
- To: mathgroup at smc.vnet.net
- Subject: [mg13865] Re: [mg13855] Summing over like elements
- From: wself at viking.emcmt.edu (Will Self)
- Date: Wed, 2 Sep 1998 01:30:59 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Mitch asks for the following: >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}} Mitch, this will do it: In[1]:= mm={{1, .5} , {2, .2} , {1, .1}} Out[1]= {{1, 0.5}, {2, 0.2}, {1, 0.1}} In[2]:= Function[x,{x,Plus@@(#[[2]]&/@Select[mm,#[[1]]==x&])}]/@ Union[#[[1]]&/@mm] Out[2]= {{1, 0.6}, {2, 0.2}} Will