I have an NxM numpy array called data.
I also have a length N array called start_indices.
I want a new length M array where the ith element is sum(data[i][start_indices[i]:]).
Here's one way to do it:
import numpy as np
data = np.linspace(0, 11, 12).reshape((3, 4))
data
array([[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11]])
start_indices = np.array([0, 1, 2])
sums = []
for start_index, row in zip(start_indices, data):
sums.append(np.sum(row[start_index:]))
sums = np.array(sums)
Is there a more numpythonic way?
np.array([np.sum(row[s:]) for s, row in zip(start,data)]). But no real difference in speed.