title: Comparing value with neighbor elements in numpy
body: <p>Let's say I have a numpy array</p>
  
  <pre><code>    a b c
  A = i j k
      u v w
  </code></pre>
  
  <p>I want to compare the value central element with some of its eight neighbor elements (along the axis or along the diagonal). Is there any faster way except the nested for loop (it's too slow for big matrix)?</p>
  
  <p>To be more specific, what I want to do is compare value of element with it's neighbors and assign new values.</p>
  
  <p>For example:</p>
  
  <pre><code>    if (j == 1):
        if (j&gt;i) &amp; (j&gt;k):
          j = 999
        else:
          j = 0
      if (j == 2):
        if (j&gt;c) &amp; (j&gt;u):
          j = 999
        else:
          j = 0
     ...
  </code></pre>
  
  <p>something like this.</p>