I have a 2D python array that I want to slice in an odd way - I want a constant width slice starting on a different position on every row. I would like to do this in a vectorised way if possible.
e.g. I have the array A=np.array([range(5), range(5)]) which looks like
array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]])
I would like to slice this as follows: 2 elements from each row, starting at positions 0 and 3. The starting posiitons are stored in b=np.array([0,3]). Desired output is thus: np.array([[0,1],[3,4]]) i.e.
array([[0, 1],
[3, 4]])
The obvious thing I tried to get this result was A[:,b:b+2] but that doesn't work, and I can't find anything that will.
Speed is important as this will operate on a largish array in a loop, and I don't want to bottleneck other parts of my code.
numpy.lib.stride_tricks... not to mention a dupe somewhere...