2023-06-12

Find the nearest quarter on numpy array?

I have a numpy array that contains numbers with a 0.25 step. So a part of this array is [-38.25, -38,-37.75, -37.5]. I want to be able to find the nearest and lowest quarter to any number.

For example if the input is -37.778 then the closest quarters would be -37.75 and -38 and the lowest would be -38. So the output should be -38 or even better the corresponding index for -38.

I do it manually with approximations and later using np.searchsorted but I thought there must be a better way. Also the array is 400 elements long so I'm not sure is the best to do it this way.



No comments:

Post a Comment