Let's say I have a 1D array of values:
T = np.array([1.3, 8.9, 1.4, 3.2, 4.4, 7.0, 2.0, 6.9]
and I have a list of start indices:
I = np.array([5, 2, 4, 1])
For each start index, I would like to grab m
consecutive values from T
starting at the start index. So, the final output for m=3
should be a 2D array with:
T = np.array([[7.0, 2.0, 6.9],
[1.4, 3.2, 4.4],
[4.4, 7.0, 2.0],
[8.9, 1.4, 3.2]])
I could simply loop through the indices but I was hoping that there were a more efficient approach:
m = 3
out = np.empty((len(I), m))
for j, i in enumerate(I):
out[j] = T[j : j+m]