title: Cumulative summation of a numpy array by index
body: <p>Assume you have an array of values that will need to be summed together</p>
  
  <pre><code>d = [1,1,1,1,1]
  </code></pre>
  
  <p>and a second array specifying which elements need to be summed together</p>
  
  <pre><code>i = [0,0,1,2,2]
  </code></pre>
  
  <p>The result will be stored in a new array of size <code>max(i)+1</code>. So for example <code>i=[0,0,0,0,0]</code> would be equivalent to summing all the elements of <code>d</code> and storing the result at position <code>0</code> of a new array of size <code>1</code>.</p>
  
  <p>I tried to implement this using</p>
  
  <pre><code>c = zeros(max(i)+1)
  c[i] += d
  </code></pre>
  
  <p>However, the <code>+=</code> operation adds each element only once, thus giving the unexpected result of </p>
  
  <pre><code>[1,1,1]
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>[2,1,2]
  </code></pre>
  
  <p>How would one correctly implement this kind of summation?</p>