[SOLVED] Inequality condition as argument of np.any() function

Issue

I’m trying to run code containing the following:

if np.any(1.5<=np.abs(Array1 - Array2)<=2):
     return True

However, when I run this, I get the error: Truth value of an array with multiple elements is ambiguous, use a.any() or a.all(). How do I implement this inequality condition in the np.any() function without error?

Solution

It seems like you can’t quite do concise inequalities with numpy like you can in the rest of Python. Instead, just do two separate conditions and combine them:

tmp = np.abs(Array1 - Array2)
if np.any((1.5 <= tmp) & (tmp <= 2)):
     return True

Answered By – richardec

Answer Checked By – Clifford M. (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *