I have an m
x n
numpy array arr
, and for each column of arr
, I have a given range of rows that I want to access.
I have an n
x 1
array vec
that describes when this range starts.
The range has some constant duration d
.
How can I extract this d
x n
array of interest efficiently?
Can this be done by clever slicing?
My initial thought was to try something like:
arr = np.tile(np.arange(10),(4,1)).T
vec = np.array([3,4,5,4])
d = 3
vec_2 = vec+d
out = arr[vec:vec2,np.arange(n)]
But this gives the following error:
TypeError: only integer scalar arrays can be converted to a scalar index
The desired output would be the following array:
array([[3, 4, 5, 4],
[4, 5, 6, 5],
[5, 6, 7, 6],
[6, 7, 8, 7])
I could loop over d
, but performance is important for this piece of code so I would prefer to vectorize it.