I have this simplified program to replace values in array which fulfill the conditions:
formula1=2*2
formula2=5*2
formula3=4*4
array = np.random.rand(2,4,10)
for n,i in enumerate(array):
if i>0.5: #find value in array with this condition
formula = formula1
array[n] = array[n]*formula #replace the found value with this value
elif i <0.1:
formula = formula2
array[n] = array[n]*formula
else:
formula = formula3
array[n] = array[n]*formula
print array
It resulted in error message:'The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()'. Any suggestion?
1 Answer 1
To enumerate an array, you can use np.ndenumerate (documentation here):
for n, i in np.ndenumerate(array):
...
answered Jun 21, 2016 at 10:41
Jacques Gaudin
17.1k11 gold badges59 silver badges80 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
iis an array. How would you determine if an array is lesser than a given value?any()checks if any of the elements in the array is lesser than the given value andall()checks if all the elements arenp.any(array<0.5)to see if any of the values inarrayare lesser than 0.5