title: Comparing elements in a numpy array, finding pairs, how to deal with edges/corners
body: <p>I'm trying to create a function below, but I'm not how to exactly to execute this. </p>
  
  <p>Let's say I have a 2D numpy array like</p>
  
  <pre><code>import numpy as np
  arr = np.array([[ 1,  2,  3,  4], [ 1,  6,  7,  8], [ 1,  1,  1, 12], [13,  3, 15, 16]])
  </code></pre>
  
  <p>This is a 4x4 matrix, which looks like this when printed:</p>
  
  <pre><code>array([[ 1,  2,  3,  4],
         [ 1,  6,  7,  8],
         [ 1,  1,  1, 12],
         [13,  3, 15, 16]])
  </code></pre>
  
  <p>I want to access the elements of <code>arr</code> and compare them to each other. For each element, I would like to see whether all surrounding eight elements (top, bottom, left, right, top-left, top-right, bottom-left, bottom-right) are greater than, less than, or equal to this element I'm at. </p>
  
  <p>I thought about using a if statement in a function like this:</p>
  
  <pre><code>if arr[i][j] == arr[i][j+1]:
      print("Found a pair! %d is equal to %d, it's in location (%d, %d)", % (arr[i][j], arr[i][j+1], i, j+1))
  elif: 
      arr[i][j] &gt; arr[i][j+1]:
          print("%d is greater than %d, it's in location (%d, %d)", % (arr[i][j], arr[i][j+1], i, j+1))
  else:
      print("%d is less than %d, it's in location (%d, %d)", % (arr[i][j], arr[i][j+1], i, j+1))
  </code></pre>
  
  <p>However, (1) I have to do this for all eight surrounding element positions and (2) I'm not sure how to write the function such that it moves from position to position correctly. Somehow one must use recursion for this to work, I think. One could possibly use a while loop as well. </p>
  
  <p>I'm planning on saving all the "pairs" with are equal, and creating a dictionary with these. </p>
  
  <p>EDIT1:</p>
  
  <p>There is still a problem I'm having to understand where the dimensions are:</p>
  
  <p>Our original matrix is shaped (4,4):</p>
  
  <p>When we compare for adjacent pairs horizontally, we find an array shaped (4,3):</p>
  
  <pre><code>arr[:-1] == arr[1:]
  
  #output 
  array([[ True, False, False, False],
         [ True, False, False, False],
         [False, False, False, False]], dtype=bool)
  </code></pre>
  
  <p>When we compare for adjacent pairs vertically, we find an array shaped (3,4):</p>
  
  <pre><code>arr[:, :-1] == arr[:, 1:]
  # output
  array([[False, False, False],
         [False, False, False],
         [ True,  True, False],
         [False, False, False]], dtype=bool)
  </code></pre>
  
  <p>When I combine these two to see whether there are pairs <em>both</em> vertically and horizontally, how do I know I am not mixing up positions? </p>