Search code examples
pythonarraysnumpyset-difference

Python, Numpy: How to select numpy array with given mask


For example, given a (10000, 250) sized numpy matrix A

>>>A.shape
(10000, 250)

and a numpy mask array m

>>>m = np.arange(0, A.shape[0], 3)
>>>m
([0, 3, 6, 9, ....., 9997])

This will select wanted column of A

>>>A[m]
>>>A[m].shape
(3333, 250)

But my question is. how to select the rest of the A? A[([1, 2, 4, 5, 7, 8, ...., 9998, 9999, 10000])]


Solution

  • You can use setdiff1d to select all indices that do not belong to m:

    A[np.setdiff1d(np.arange(A.shape[0]), m)]