title: How to create an array of neighbors out from each element in an numpy 2d array
body: <p>I am trying to create an array of neighbors for each element in an array. Is there a way to use vectorisation techniques to speed it up?</p>
  
  <p>I've used a nested for loop to iterate through each element in the array to create.</p>
  
  <p>Initial array</p>
  
  <pre><code>self.grid = np.zeros((256,256), np.uint)
  </code></pre>
  
  <p>Array of neighbors out  of the initial array</p>
  
  <pre><code>        for i in range(256):
              for j in range(256):
  
                  grid = [[self.grid[i-1][j-1], self.grid[i][j-1], self.getGrid[i+1][j-1]],
                          [self.grid[i-1][j], self.grid[i][j], self.grid[i+1][j]],
                          [self.grid[i-1][j+1], self.grid[i][j+1], self.grid[i+1][j+1]]]
  </code></pre>
  
  <p>(This code doesn't account for edge cases)</p>