I am trying to index neighbours of a particular numpy array item. For example if I have the array shown below and I check for items over a certain value how can I index the cell above, below, left, and right of true items in an efficient manner without resorting to loops etc.
In [34]: x
Out[34]:
array([[ 10., 10., 10., 10., 10.],
[ 10., 10., 10., 10., 10.],
[ 10., 20., 10., 10., 10.],
[ 10., 10., 10., 20., 10.],
[ 10., 10., 10., 10., 10.]])
In [37]: ans = x > 10
In [38]: ans
Out[38]:
array([[False, False, False, False, False],
[False, False, False, False, False],
[False, True, False, False, False],
[False, False, False, True, False],
[False, False, False, False, False]], dtype=bool)
This will give you the indices of the neighbors: