I have a 2D numpy array and I need to select ranges from each row of that array, i.e. I need to do something like:
arr = arr[:, 3:6]
But instead of 3
and 6
I would like to use two 1D arrays, which contain starting and ending indices of required ranges.
arr
rows are packed of coordinates data.- The length of each range is the same and is known beforehand.
- Starting and ending indices are aligned to the length of range (e.g. if the length of packed data is 3 then the length of the range is also 3 and starting and ending indices are multiples of 3)
How can I do that in numpy (I need it to be efficient vectorised implementation)?
Example:
arr = np.array([[0,1,2,3],
[4,5,6,7]])
str_idx = [0, 2]
end_idx = [2, 4]
Wanted result:
selected = [[0,1],
[6,7]]