0

Given the numpy array a

a = [[[0 0] [1 0] [2 0]]
     [[0 1] [1 1] [2 1]]
     [[0 2] [1 2] [2 2]]]

And the list b

b = [[1, 0], [2, 0]]

How can I get the mask c

c = [[False True True]
     [False False False]
     [False False False]]
2
  • Could you post the code that creates the numpy array Commented Sep 30, 2019 at 15:11
  • a = np.dstack((np.meshgrid(x, y))), where x = y = [0, 1, 2] Commented Oct 1, 2019 at 13:19

1 Answer 1

1

you can use numpy broadcast feature to compare each number pair in b with all pairs on b like below

## np.newaxis add a new dimension at that position. missing dimension (i.e 
## dimension with size 1) will repeat to match size of corresponding dimension

a = np.asarray([[[0, 0], [1, 0], [2, 0]],
     [[0, 1], [1, 1], [2, 1]],
     [[0, 2], [1, 2], [2, 2]]])[:,:,np.newaxis,:]

b = np.array([[1, 0], [2, 0]])[np.newaxis,:,:]

(a == b).all(axis=3).any(axis=2)

Result

array([[False,  True,  True],
       [False, False, False],
       [False, False, False]])
Sign up to request clarification or add additional context in comments.

1 Comment

I know that comments are used to ask for clarification or to point out problems in the post, but thank you.

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.