3

Suppose I have a column vector y with length n, and I have a matrix X of size n*m. I want to check for each element i in y, whether the element is in the corresponding row in X. What is the most efficient way of doing this?

For example:

y = [1,2,3,4].T and

X =[[1, 2, 3],[3, 4, 5],[4, 3, 2],[2, 2, 2]]

Then the output should be

[1, 0, 1, 0] or [True, False, True, False] 

which ever is easier.

Of course we can use a for loop to iterate through both y and X, but is there any more efficient way of doing this?

1
  • Are you sure you don't want the output to be np.array([True, False, True, False])? Commented Oct 21, 2016 at 11:30

1 Answer 1

4

Vectorized approach using broadcasting -

((X == y[:,None]).any(1)).astype(int)

Sample run -

In [41]: X        # Input 1
Out[41]: 
array([[1, 2, 3],
       [3, 4, 5],
       [4, 3, 2],
       [2, 2, 2]])

In [42]: y        # Input 2
Out[42]: array([1, 2, 3, 4])

In [43]: X == y[:,None] # Broadcasted  comparison
Out[43]: 
array([[ True, False, False],
       [False, False, False],
       [False,  True, False],
       [False, False, False]], dtype=bool)

In [44]: (X == y[:,None]).any(1) # Check for any match along each row
Out[44]: array([ True, False,  True, False], dtype=bool)

In [45]: ((X == y[:,None]).any(1)).astype(int) # Convert to 1s and 0s
Out[45]: array([1, 0, 1, 0])
Sign up to request clarification or add additional context in comments.

1 Comment

I'd argue that any(1) should be spelt .any(axis=1) for clarity

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.