Skip to content
Advertisement

Finding location of subarray in numpy array

I want to find the location of a subarray in an numpy array.
For example, if the large array is of shape (10000, 10, 10, 10), namely 10000 arrays of shape (10,10,10) and I want to find the location of the subarray of shape (3,3,3) as follows:

JavaScript

So I will know that at the location of n, i, j, k I will get:
largearray[n, i:i+subarray.shape[0], j:j+subarray.shape[1], k:k+subarray.shape[2]] == subarray

Advertisement

Answer

Long ago I made a numpy-only function for this, but skimage.util.view_as_windows is a bit easier to implement.

JavaScript

That gives coordinates for 2 locations with the 3x3x3 zeros box in my random input.

Why do this as windows? Because looping takes forever in python and making the windows as a copy will kill your RAM. view is just another way to address memory in test that is more useful for this type of comparison.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement