Suppose I have an array A. I have a series of index pairs (a1, b1), (a2, b2) ... (an, bn)
I want to obtain all the sums of the elements between those pairs. i.e.
Assuming your index pairs are stored in a NumPy array indices of shape (n, 2) and n is fairly large, it is probably best to avoid any Python loop:
indices
(n, 2)
n
c = numpy.r_[0, A.cumsum()][indices] sums = c[:,1] - c[:,0]