python - Need to grab a 3x3 neighbourhood of an input cell from a 2d numpy array -


i trying define function return 3x3 neighbourhood of input cell. right have:

def queen_neighbourhood(in_forest, in_row, in_col):      neighbourhood = in_forest[in_row-1:in_row+1, in_col-1:in_col+1]      return neighbourhood 

(in_forest input array).

when run this, seems return 2x2 matrix, instead of 3x3. why this? seems me inputting row , column reference, , slicing out square starts 1 row behind input row, , ends 1 row ahead of it, , same columns.

so example, given input array such:

[ 01, 02, 03, 04, 05   06, 07, 08, 09, 10   11, 12, 13, 14, 15   16, 17, 18, 19, 20   21, 22, 23, 24, 25 ] 

and using row 2, col 3, want return matrix such:

[ 02, 03, 04   07, 08, 09   12, 13, 14 ] 

when in_forest[in_row-1:in_row+1, in_col-1:in_col+2] saying "give me square in_row-1 inclusive in_row+1 exclusive, , in_col-1 inclusive in_col+2 exclusive. slices to, not including second index.

simply use in_row-1:in_row+2 , in_col-1:in_col+2 instead slice including "+1"s.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -