Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the relative complement of a boolean numpy array and another?

Let's say I have two numpy arrays:

>>> v1
array([ True, False, False, False,  True])
>>> v2
array([False, False,  True,  True,  True])

I'm trying to retrieve an array that has the same length (5) and contains True in each position where v1==True AND v2==False. That would be:

array([True, False,  False,  False,  False])

Is there a quick way in numpy, something like logical_not() but considering v1 as the reference and v2 as the query?

like image 807
schmat_90 Avatar asked Dec 04 '25 03:12

schmat_90


1 Answers

You just need to use the right bitwise operators:

v1 & ~v2
# array([ True, False, False, False, False])
like image 138
yatu Avatar answered Dec 05 '25 16:12

yatu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!