I am trying to iterate over numpy arrays and generate an output, which has conditions similar to that described below:
min1 = 3
max1 = 1
a1 = np.array([1, 2, 5, 3, 4])
a2 = np.array([5, 2, 6, 2, 1])
output = np.zeros(5)
for i in range(0, 5):
if((a1[i] - a2[i]) > min1):
output[i] = 3 * (a1[i] - a2[i])
if((a1[i] - a2[i]) < max1):
output = 5 * (a1[i] - a2[i])
I need to optimize the above code, so that I utilize the numpy functionalities as the best and also avoid using a loop. How should I do it?