1

I have a number of arrays of same length:

a = [3,7,5,2,7]
b = [3,4,1,6,8]
c = [2,3,7,8,3]
d = [1,3,5,6,4]
e = [2,6,5,1,7]

My condition is x > 2. What I need is a final array checking if the condition applies for every position of all arrays.

The result would be:

[False, True, False, False, True]

Or even better:

[0, 1, 0, 0, 1]

Sorry if this is simple, I searched a long time but only found related topics but none exactly answering this.

1 Answer 1

2

Stack those 1D input arrays as rows of a 2D array with np.vstack, perform the comparison and then use np.all along the first axis. Thus, the implementation would be -

(np.vstack((a,b,c,d,e))>2).all(axis=0)

Sample run -

>>> np.vstack((a,b,c,d,e)) # Stack as a 2D array
array([[3, 7, 5, 2, 7],
       [3, 4, 1, 6, 8],
       [2, 3, 7, 8, 3],
       [1, 3, 5, 6, 4],
       [2, 6, 5, 1, 7]])
>>> np.vstack((a,b,c,d,e))>2
array([[ True,  True,  True, False,  True],
       [ True,  True, False,  True,  True],
       [False,  True,  True,  True,  True],
       [False,  True,  True,  True,  True],
       [False,  True,  True, False,  True]], dtype=bool)
>>> (np.vstack((a,b,c,d,e))>2).all(axis=0)
array([False,  True, False, False,  True], dtype=bool)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.