2

I have a two-dimensional (2D) array that contains many one-dimensional (1D) arrays of random boolean values.

import numpy as np

def random_array_of_bools():
  return np.random.choice(a=[False, True], size=5)


boolean_arrays = np.array([
  random_array_of_bools(),
  random_array_of_bools(),
  ... so on
])

Assume that I have three arrays:

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

This is my desired result:

[False, False, True, False, False]

How can I achieve this with NumPy?

0

3 Answers 3

2

Use min with axis=0:

>>> boolean_array.min(axis=0)
array([False, False,  True, False, False])
>>> 
Sign up to request clarification or add additional context in comments.

Comments

2

Use .all:

import numpy as np

arr = np.array([[True, False, True, True, False],
                [False, True, True, True, True],
                [True, True, True, False, False]])


res = arr.all(0)
print(res)

Output

[False False  True False False]

1 Comment

Thank you! I will stick with using .min(axis=0) for now, but I see how that works as well!
0

try numpy bitwise_and =>

out_arr = np.bitwise_and(np.bitwise_and(in_arr1, in_arr2),in_arr3) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.