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

Find absolute difference value between elements using just NumPy array operations

a = np.array([101,105,90,102,90,10,50])
b = np.array([99,110,85,110,85,90,60])

expected result = np.array([2,5,5,8,5,20,10])

How can I find minimum absolute difference value between elements using just numpy operations; with modulus of 100 if two values are across 100.

>Solution :

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

One way to do this is to use np.minimum() with np.abs():

import numpy as np

a = np.array([101, 105, 90, 102, 90, 10, 50])
b = np.array([99, 110, 85, 110, 85, 90, 60])

print(np.minimum(np.abs(a - b), np.abs((a - b) % 100)))

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