0

I have two arrays of type float (compare and hh3). I want to compare each element of "compare" to that of "hh3" and the result gets added to another vector pp.

I have searched for the error on this website and I know the reason for it being the "if a>b" comparison doesn't work if a and b are arrays

#hh3 is a array of length 70
#pp is an zero vector of a specified length
K = 0
compare = np.arange(1, 2, 1e-4)
compare_size = len(compare)

for j in range(compare_size):
     for i in range(no*nos):
        if hh3[i] >= compare[j]:
          pp[K] = pp[K] + 1
        if pp[K] == 0:
          break
      K = K + 1

In the end I want to compare each i element of hh3 to each j element of compare. The error occurs in "if hh3[i] >= compare[j]:"

  • 3
    Please complete the code with some values for `hh3`, with which the error can be reproduced. While you're at it, remove the code which is not needed for the error. See [how to create a minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – zvone Oct 03 '19 at 15:21
  • 1
    Could this be any help? https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous – incapaz Oct 03 '19 at 15:25
  • @incapaz Thank you for the suggestion. I did have a look at it before but unfortunately didn't solve my error. – Devgeet Patel Oct 03 '19 at 15:52

1 Answers1

1

This type of error occurs when you give the if condition a table/list of booleans instead of a boolean scalar.

In your case, you probably have either hh3[i] >= compare[j] or pp[K] == 0 that returns an array of booleans.

You may want to verify that hh3.shape and pp.shape are of the type (value,).

Maxence Bouvier
  • 141
  • 1
  • 6