1

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?

6
  • Can you add the complete traceback? Commented Jun 21, 2016 at 10:34
  • @syntonym if i<0.5: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() Commented Jun 21, 2016 at 10:37
  • In your for loop, i is 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 and all() checks if all the elements are Commented Jun 21, 2016 at 10:38
  • @SvbZ3r0 isn't any() or all() used to find boolean value of True and False only? Commented Jun 21, 2016 at 10:41
  • Not necessarily. Try np.any(array<0.5) to see if any of the values in array are lesser than 0.5 Commented Jun 21, 2016 at 10:44

1 Answer 1

3

To enumerate an array, you can use np.ndenumerate (documentation here):

for n, i in np.ndenumerate(array):
    ...
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.