4

Is there a fast way to find all indices where a 2d array is inside a 3d array?

I have this 3d numpy array:

arr = np.array([
        [[0,1],[0,2],[0,3],[0,4],[0,4],[0,5],[0,5],[0,5],[0,5],[0,5]],
        [[0,1],[0,2],[0,2],[0,2],[0,3],[0,4],[0,4],[0,4],[0,5],[0,5]],
        [[0,1],[0,2],[0,3],[0,3],[0,3],[0,4],[0,4],[0,5],[0,5],[0,5]]
       ])

And I would like to find all indices where [0,4] occurs. I've tried this one:

whereInd = np.argwhere(arr == np.array([0,4]))

but it doesn't work. The expected result is:

[[0 3],[0 4],[1 5],[1 6],[1 7],[2 5],[2 6]]

Another question is, will this be fast? Because I would like to use it for a (10000,100,2) array.

2 Answers 2

2

Using argwhere() is a good idea, but you also need to use all() to get your desired output:

>>> np.argwhere((arr == [0, 4]).all(axis=2))
array([[0, 3],
       [0, 4],
       [1, 5],
       [1, 6],
       [1, 7],
       [2, 5],
       [2, 6]])

Here all() is used to check each row is [True, True] following the comparison (that is, the row is equal to [0, 4]). In a 3D array, axis=2 points along rows.

This reduces the number of dimensions to two, and argwhere() returns the desired array of indices.


Regarding performance, this method should handle arrays of the size you specify fairly quickly:

In [20]: arr = np.random.randint(0, 10, size=(10000, 100, 2))
In [21]: %timeit np.argwhere((arr == [0, 4]).all(axis=2))
10 loops, best of 3: 44.9 ms per loop
Sign up to request clarification or add additional context in comments.

Comments

0

The simplest solution I can think of is:

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

whereInd = []
for i,row in enumerate(arr):
    for j,elem in enumerate(row):
        if all(elem == [0,4]):
            whereInd.append((i,j))

print whereInd
#prints [(0, 3), (0, 4), (1, 5), (1, 6), (1, 7), (2, 5), (2, 6)]

Although any solution with np.argwhere should work around 10 times faster.

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.