NxN python arrays subsets
Question: I need to carry out some operation on a subset of an `NxN array`. I have the
center of the sub-array, `x` and `y`, and its size.

So I can easily do:

`subset = data[y-size:y+size,x-size:x+size]`

And this is fine.

What I ask is if there is the possibility to do the same without writing an
explicit loop if `x` and `y` are both 1D arrays of positions.

Thanks!


Answer: Using a simple example of a 5x5 array and setting _size=1_ we can get:

    
    
    import numpy as np
    data = np.arange(25).reshape((5,5))
    
    size = 1
    x = np.array([1,4])
    y = np.array([1,4])
    subsets = [data[j-size:j+size,i-size:i+size] for i in x for j in y]
    
    print(subsets)
    

Which returns a list of numpy arrays:

    
    
    [array([[0, 1],[5, 6]]),
     array([[15, 16],[20, 21]]),
     array([[3, 4],[8, 9]]),
     array([[18, 19],[23, 24]])]
    

Which I hope is what you are looking for.

