1
$\begingroup$

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...

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

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
subs = A[::2,::2]+2*A[::2,1::2]+4*A[1::2,::2]+8*A[1::2,1::2]
allowed = ... # List of submatrices to count expressed in binary
for a in allowed:
  print('count %d = %d'%(a,sum(subs==a))
| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.