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!

Mazdak
  • 105,000
  • 18
  • 159
  • 188
Trevor Wise
  • 21
  • 1
  • 3

1 Answers1

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.

anishtain4
  • 2,342
  • 2
  • 17
  • 21
  • 1
    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? – Filip Apr 25 '21 at 22:21