I have two arrays of incidies with shape m
. I need to take the mean of the values inbetween the indicies from an array with shape m x n
. Can this be done without iterating through each row? What is the fastest way to do this?
idx0 = np.array([1, 3, 2, 5])
idx1 = np.array([5, 8, 6, 7])
d = np.array([[1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,8,9]])
Desired result:
<< d[np.arange(d.shape[0]), idx0:idx1]
>> np.array([3.5, 6, 4.5, 6.5])
The best ways I've found to do this are list comprehension or using a for loop with numba, but this seems like it should be a common problem? Thanks.
In your case you'd still need to get slices of pairwised ranges of indices from idx0
and idx1
.zip
+ slice
is a straigtforward way:
[d[:, slice(*a)].mean() for a in zip(idx0, idx1)]
[3.5, 6.0, 4.5, 6.5]