MathGroup Archive 1998

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

Search the Archive

Re: Summing over like elements


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


  • Next by Date: RE: An operator called-- LocalSet
  • Next by thread: Re: Summing over like elements