3

I'm stuck with the problem of testing whether or not a 2d array appears in the exact same order in another 2d array, e.g.:

01
23

in

000100
002300
000000
000000

would return true, but in

001000
002300
000000
000000

would return false.

I've already attempted to use scipy.signal.correlate2d but to no avail. Any help? Cheers!

)edit) Code for generating the data:

import numpy as np
a = np.array([[0,0,0,1,0,0],
             [0,0,2,3,0,0],
             [0,0,0,0,0,0],
             [0,0,0,0,0,0]])

a = np.array([[0,0,1,0,0,0],
             [0,0,2,3,0,0],
             [0,0,0,0,0,0],
             [0,0,0,0,0,0]])



b = np.array([[0,1],
             [2,3]])
axequesty2
  • 59
  • 4

1 Answers1

2
from skimage.util import view_as_windows

windows = view_as_windows(a, b.shape)

print((windows == b).all(axis=(2,3)).any())

would be the answer, as stated in How can I check if one two-dimensional NumPy array contains a specific pattern of values inside it?

mozway
  • 194,879
  • 13
  • 39
  • 75
axequesty2
  • 59
  • 4