Suppose I have a numpy 2D array and I want to index or extract a region of it using another 2D array which provides the index pairs, similar as in https://stackoverflow.com/a/14999290/17173241::
a = np.arange(36).reshape(6,6)
>>> a
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35]])
b = np.array([[0,1],[1,2],[2,1],[3,3],[4,4],[5,3]])
>>> b
array([[0, 1],
[1, 2],
[2, 1],
[3, 3],
[4, 4],
[5, 3]])
>>> a[b[:,0],b[:,1]]
array([ 1, 8, 13, 21, 28, 33])
this works as intended. Now I want to use the second index of b as e.g. the mid point of a range, to extract not only single points in a for each row, but lets say 3 points together per row. How do I do it?
I tried
>>> a[b[:,0],b[:,1]-1:b[:,1]+1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: only integer scalar arrays can be converted to a scalar index
but this gives a TypeError, which I cannot think through..
I came up with this attempt, because I thought it is similar to
>>> a[b[:,0],1:3]
array([[ 1, 2],
[ 7, 8],
[13, 14],
[19, 20],
[25, 26],
[31, 32]])
although of course the last example uses always the same indices as the second index.
I also tried this:
>>> a[b[:,0],(b[:,1]-1,b[:,1]+1)]
array([[ 0, 7, 12, 20, 27, 32],
[ 2, 9, 14, 22, 29, 34]])
yields the lower and upper bound of my range as it is read as a tuple, I guess. I want to achieve something like
array([[ 0, 7, 12, 20, 27, 32],
[ 1, 8, 13, 21, 28, 33]
[ 2, 9, 14, 22, 29, 34]])
or even better
array([[ 0, 1, 2],
[ 7, 8, 9],
[12, 13, 14],
[20, 21, 22],
[27, 28, 29],
[32, 33, 34]])
BONUS: for a range that wide, that indices become negative i.e., b[:,1]-2:b[:,1]+1]
,
how would I avoid getting values from the end of each row, but instead getting zeros.