-1

I have two arrays. Both are 1D. However, I am getting the following Value Error. Below is what I tried.

R=np.arange(30,50,1)
T=np.arange(70,90,1)

H=[]

if (T > 8) and (R>10): 
   H.append(0.5 * (T + 61. + (T - 68.) * 1.2 + R * 0.094))
else:
   H.append(0 * 2)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Aviv Yaniv
  • 6,188
  • 3
  • 7
  • 22
Chane
  • 45
  • 7
  • Please include `T` and `R` so that your problem is reproducible – 7koFnMiP Sep 01 '20 at 09:56
  • @7koFnMiP . I added them, thanks. – Chane Sep 01 '20 at 10:02
  • Have you seen this? [NumPy Error: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()](https://stackoverflow.com/questions/54191677/numpy-error-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous) – Carlo Zanocco Sep 01 '20 at 10:04

3 Answers3

0

Python struggled to compare the values in the lists T to number.
The error is raised on T > 8.

For Lists

  1. Use all() to check if all items in the list are above value:
all(x > value for x in list)
  1. Use any() to check if any item in the list are above value:
any(x > value for x in list)

For NumPy Array

  1. When having NumPy array, this can be shortened to:
T = np.array([1,2,3])
all(T > 8)

Fix

Fix to all:

H=[]

import numpy as np

T = np.array([30,34,56])
R = np.array([29,500,43])

if all(T > 8) and all(R > 10):
    H.append(0.5 * (T + 61. + (T - 68.) * 1.2 + R * 0.094))
else:
    H.append(0 * 2)
Aviv Yaniv
  • 6,188
  • 3
  • 7
  • 22
0

Like the above answer states, you can use all or any. The fix using any is:

if any(t > value for t in T) and any(h > value for h in H):
   H.append(0.5 * (T + 61. + (T - 68.) * 1.2 + R * 0.094))
else:
   H.append(0 * 2)

The python interpreter needs 1 value at a time so that it can compare.

David Duran
  • 1,786
  • 1
  • 25
  • 36
0

Simple as that.

import numpy as np
R=np.arange(30,50,1)
T=np.arange(70,90,1)

H=[]

if (T > 8).all() and (R>10).all():
   H.append(0.5 * (T + 61. + (T - 68.) * 1.2 + R * 0.094))
else:
   H.append(0 * 2)

print(H)

Here is an explanation.

If you really just want to know if atleast one element falls under the condition you should use any().

Josef
  • 2,869
  • 2
  • 22
  • 23