Efficient identification of adjacent elements in numpy matrix
<p>I have a 100 by 100 numpy matrix. The matrix is mostly filled with zeros, but also contains some number of ints. For example: </p>

<pre><code>[0 0 0 0 0 0 0 1]
[0 2 2 0 0 0 0 0]
[0 0 2 0 0 0 0 0]  False
[0 0 0 0 0 0 0 0]
[0 3 3 0 0 0 0 0]
</code></pre>

<p><strong>What is the most efficient way to identify whether the matrix contains any number of adjacent ints of distinct types?</strong> </p>

<p>The above example would return False. Here is a True example, with the row containing the adjacency indicated:</p>

<pre><code>[0 0 0 0 0 0 0 1]
[0 2 2 1 1 0 0 0]   &lt;----  True
[0 0 2 0 0 0 0 0]  
[0 0 0 0 0 0 0 0]
[0 3 3 0 0 0 0 0]
</code></pre>

<p>Diagonals do not count as being adjacent. So this example would also return False:</p>

<pre><code>[0 0 0 1 1 1 1 1]
[0 2 2 0 1 0 0 0]
[0 0 2 0 0 0 0 0]   False
[0 3 0 0 0 0 0 0]
[3 3 3 0 0 0 0 0]
</code></pre>

<p>I do not need to identify the position of the adjacency, just whether it exists of not.</p>

<p>Currently, I cannot do better than finding each no-zero element in the matrix and then checking its 4 flanking elements.  </p>

<p><strong>Thanks for all the great answers.</strong> </p>