1

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]]
Rizhiy
  • 775
  • 1
  • 9
  • 31
  • Can you give an example of the starting and ending index arrays? are you looking for something like start_ind = [1,4,2] end_ind = [5,8,6] so arr[:, start_ind:end_ind] will give you 1:5 for the first row, 4:8 for the second row, etc? – kkawabat Jul 02 '18 at 18:52
  • Solution to question given by @Divakar worked for me. – Rizhiy Jul 02 '18 at 19:17

0 Answers0