I interpenetrate your task in the following way: Find the min of x/y
with the condition that the result is still positive.
Therefore I would mask the array to get rid of all the cases we don't want to consider, such as one of the number is negative or the element of y
is zero.
You could do that by setting those positions in the array to np.nan
:
x = np.array([1,2,3,4,5], dtype=np.float)
y = np.array([-1,0,1,2,3], dtype=np.float)
y_cleared = np.copy(y)
y_cleared[y == 0] = np.nan # get rid of zeros
y_cleared[y < 0] = np.nan # get rid of negative values
y_cleared[x < 0] = np.nan # get rid of negative values
y_cleared
>>> array([nan, nan, 1., 2., 3.])
Then there are special numpy methodes to work with arrays and ignoring np.nan
like:
np.nanmin(x/y_cleared)
>>> 1.6666666666666667