I have two NumPy arrays, say num and denom. I need to return specific values, based on if the respective elements are zero or not, in num and denom,
r2 = []
for denind, denel in enumerate(denom):
numel = num[denind]
if denel: # Denominator is not zero
r2.append(1 - numel/denom)
elif numel: # Denominator is zero, but numerator is not zero
r2.append(0.0)
else: # Both denominator and numerator are zero.
r2.append(1.0)
return np.array(r2)
Is there a "NumPy" way of doing such an iteration.