Given a 1-D array, A and a list of n unique and sorted indices idx, I would like to calculate sums of A[idx[i]:idx[i + 1]] for i = 0 to n - 1. A for-based solution is:
S = [A[idx[i]:idx[i + 1]].sum() for i in range(n - 1)]
But I suppose this would be very slow if n is large as it is done at Python level. Is there a NumPy function to achieve this (hopefully faster)?