1

I've been trying to use numpy on Python to plot some data. However I'm getting an error I don't understand:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

And this is the line supposed to cause the error (the third line):

def T(z):
for i in range(3):
    if (z <= z_tbl[i+1]):
        return T0_tbl[i]+a_tbl[i]*(z-z_tbl[i])
return 0

Those lists are just some lists of integers, and z is an integer too

How can i fix it?

3
  • What's z and z_tbl? Commented Feb 28, 2016 at 22:07
  • z_tbl=[0,11000,20000,32000,47000] and z is an integer Commented Feb 28, 2016 at 22:12
  • related?: stackoverflow.com/questions/10062954/… Commented Sep 15, 2020 at 20:40

2 Answers 2

5

Either z or z_tbl[i+1] is a numpy array. For numpy arrays, rich comparisons (==, <=, >=, ...) return another (boolean) numpy array.

bool on a numpy array will give you the exception that you are seeing:

>>> a = np.arange(10)
>>> a == 1
array([False,  True, False, False, False, False, False, False, False, False], dtype=bool)
>>> bool(a == 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Numpy is trying to tell you what to do:

>>> (a == 1).any()  # at least one element is true?
True
>>> (a == 1).all()  # all of the elements are true?
False
Sign up to request clarification or add additional context in comments.

1 Comment

So how is that my z_tbl is a numpy array? I just don't understand why the comparison returns another array. It should return a boolean, shoudln't it?
0

For the if condition use this

if (z <= z_tbl.item(i+1)):

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.