简体   繁体   中英

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

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]]     

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM