0

I have a big 2d bitewise array (0s and 1s only) and a small one (3x3).

I want to see where the big array matches the small one, i.e. i, j for which

big_array[i-1:i+2, j-1:j+2] == small_array

There could be more than one (i,j) that satisfies the condition. How do I do this without writing a double nested i, j loop?

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
fmonegaglia
  • 2,749
  • 2
  • 24
  • 34
  • I adapted this answer to my case. stride_tricks is very fast. http://stackoverflow.com/questions/4936620/using-strides-for-an-efficient-moving-average-filter – fmonegaglia Nov 13 '14 at 10:44

1 Answers1

2

I hope, this code is what you need:

import numpy as np

big_array = np.array(...)
small_array = np.array(...)

for (i, j), _ in np.ndenumerate(big_array[:-2, :-2]):
    if (big_array[i:i+3, j:j+3] == small_array).all():
        print (i, j)
erthalion
  • 3,094
  • 2
  • 21
  • 28