0

Consider an array:

array([[  0,   0],
        [  0,   0],
        [  0,   0],
        [  0, 255],
        [255, 255],
        [255, 255],
        [255,   0],
        [  0,   0],
        [  0,   0],
        [  0,   0],
        [  0,   0],
        [  0, 255],
        [255, 255],
        [255,   0],
        [  0,   0],
        [  0,   0]], dtype=uint8))

I would like to compare this with [255, 0] and get an output like this:

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

How can I achieve this using numpy, because when I perform arr == [255, 0], the shape that I get is (N, 2), meaning it is an element wise comparision:

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

What I tried was to using np.apply_along_axis function with np.logical_and, but since the and function takes two arguments, my row is considered as one argument, therefore it gives an invalid argument error:

np.apply_along_axis(lambda x: np.logical_and(x), 1, arr==[255,0])
Value Error: Invalid number of arguments
Murtaza Raja
  • 309
  • 4
  • 15

1 Answers1

0
np.all(arr == [255, 0], axis=1)

Output

array([False, False, False, False, False, False,  True, False, False,
       False, False, False, False,  True, False, False])
Reti43
  • 9,656
  • 3
  • 28
  • 44