Home > database >  How to find the true and false by matching the elements in an array
How to find the true and false by matching the elements in an array

Time:08-31

I have an array like a and b.
a is reference array for matching with b needs to do count where: array a element value 0 is matches the element value 0 in array b as true and array a elemet value 0 is matches with element value 1 in array b as true

a=np.array([[0,0,1,1,1],
            [1,0,1,1,1],
            [1,1,0,1,0],
            [0,1,1,0,1]])

b=np.array([[1,0,0,1,0],
            [0,0,0,1,0],
            [1,0,0,1,1],
            [0,1,0,1,0]])

EXpected output is like :

[[True,True,False,False,False],
[False,True,False,False,False],
[False,False,True,False,True],
[True,False,False,True,False]]     

CodePudding user response:

If you want a==0 & b==0 -> True OR a==0 & b==1 -> True, else False, then the value of b doesn't matter (assuming you only have 0/1 as values).

You just want a==0:

a==0

or

~a.astype(bool)

output:

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

If b can contain values other that 0/1, then use:

a==0 & np.isin(b, [0,1])
  • Related