2

Is there a way I can check to see whether a matrix in Numpy contains a specific vector?

i.e.

X = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]) 

v = np.array([1, 1, 1])

I want to be able to test: bool = v in X. I know this doesn't work with Numpy and wonder if there is a ay to test this without obnoxious loops? Thanks for any help!

0

1 Answer 1

0

You can aggregate the rows with all then see if there's any row where all the columns have matched.

np.any(np.all(np.isin(X,v,True),axis=1))

I should mention, this is assuming that your rows are distinct and unique.

Sign up to request clarification or add additional context in comments.

1 Comment

This is incorrect. Consider the example X = np.array([[ 0, 1],[ 1, 0], [ 2, 3], [11, 11]]) and b = np.array([11,10]). Or have I misunderstood something here?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.