Home > Net >  Comparing a boolean array with a list of boolean arrays and returning the index (non-elementwise com
Comparing a boolean array with a list of boolean arrays and returning the index (non-elementwise com

Time:10-20

If I have a boolean array (A) and want to compare it with a list of boolean arrays (B):

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

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

I want to return the index where the two arrays match (whole array comparison not elementwise comparison) e.g. how would I get the output = [False, True, False] or [0, 1, 0] would also be acceptable?

CodePudding user response:

You need first check this two array then use .all(axis=1) for check each row like below:

>>> from numpy import array
>>> A = array([ True,  True, False, False, False, False,  True])

>>> B = [array([ True,  True,  True, False, False, False,  True]), 
...      array([ True,  True, False, False, False, False,  True]),
...      array([False,  True, False, False, False, False, False])]


>>> A == B
array([[ True,  True, False,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True,  True,  True],
       [False,  True,  True,  True,  True,  True, False]])


>>> (A == B).all(1)
array([False,  True, False])

CodePudding user response:

from numpy import array
import numpy as np

A = array([ True,  True, False, False, False, False,  True])
B = [array([ True,  True,  True, False, False, False,  True]), 
    array([ True,  True, False, False, False, False,  True]),
    array([False,  True, False, False, False, False, False])]


res = []

for x in B:
    res.append(np.array_equal(A, x))

print(res)
  • Related