# detecting special $2 \times 2$ matrices in a large array of zeros and ones

I have a large array of zeros and ones and I need to count instances of

0 1     1 0     0 0     1 1
0 1,    1 0,    1 1,    0 0


And I would like to exclude all other $2 \times 2$ submatrices... such as

0 1     1 1
0 0,    1 0,    etc.


In the space of $2 \times 2$ binary matrices has $2^4 = 16$ elements, and I could like to single out these 4 matrices. I can't use a linear condition since

0 1     0 0   0 1
0 1  +  1 1 = 1 0


It is possible to explicitly write a boolean that checks these explicitly, but then I lose the wonderful numpy quickness...

• this looks suspiciously like edge detection, in which case you might find docs.scipy.org/doc/scipy/reference/ndimage.html handy
– k20
Mar 4 '14 at 18:58
• What do you mean by exclude? Do you mean adjacent submatrix or arbitrary? Mar 4 '14 at 19:15

Assuming you are trying to rule out adjacent submatrices, you can build an array that represents the $2 \times 2$ submatrices in binary and then check against a list of the valid types. I.e.,
# Represent 2 by 2 submatrices of A in binary