1

There is a numpy array() with m x n size, and I want to get indexes that match a particular pattern.

Since the map would be bigger than the example below, I guess solving with loop is not efficient.

In this example, I want to get the following as result:

[
   [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3)],
   [(1, 3), (1, 4), (1, 5), (1, 6), (2, 3), (2, 4), (2, 5), (2, 6)]
]

Is there any tool to do this easily?

2
  • Please, avoid posting images of text. It is a better practice to transcribe them instead. Also, bear in mind that map is a standard Python function, so when you use that name as a variable, you are overwriting it. Commented Feb 26, 2022 at 13:36
  • 1
    @accdias Thanks for your advice and assistance. I'll keep that in mind Commented Feb 26, 2022 at 14:02

1 Answer 1

2

You can efficiently solve this problem using a convolution where the filter is:

[[1, 0, 0, 0],
 [1, 1, 1, 1]]

This can be done efficiently with scipy.signal.convolve2d.

The resulting array can then be filtered by looking for values greater than the sum of the previous filter (ie. 5). This can be done with np.where(result >= 5).

Note that for big pattern, you can use an FFT-based convolution.

To summarize, you can do:

np.where(scipy.signal.convolve2d(m, np.flip(f), mode='valid') >= 5)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.