Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Inequality condition as argument of np.any() function

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading