Assume you have an array of values that will need to be summed together
d = [1,1,1,1,1]
and a second array specifying which elements need to be
Juh_'s comment is the most efficient solution. Here's working code:
import numpy as np import scipy.ndimage as ni i = np.array([0,0,1,2,2]) d = np.array([0,1,2,3,4]) n_indices = i.max() + 1 print ni.sum(d, i, np.arange(n_indices))