Given numpy ndarray A and an integer array I, of the same shape, with highest value imax and an array B = np.zeros(imax) we can do B[I] = A. However if I has repeated entries the last assignment holds. I need to do this while summing over repeated entries instead, like
For i in range(A.size):
B[I.ravel()[i]] += A.ravel()[i]
Is there a good way to do this in numpy?
For example, I want this behavior (but neither = nor += works like this)
A = np.array((1,2,5,9))
I = np.array((0,1,2,0),dtype=int)
B = np.zeros(3)
B[I] += A
print(B)
>>> array([10,2,5])
Here we see 1+9=10 in the first entry.
add.atprovides an un-buffered addition for use when you have index duplicates