1

I have two vectors (or two one dimensional numpy arrays with the same number of elements) a and b where I want to find the number of cases I have that:

a < 0 and b >0

But when I type the above (or something similar) into IPython I get:

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

How am I supposed to do the above operation?

Thank you

luffe
  • 1,588
  • 3
  • 21
  • 32
  • This question is more or less the same as [this one](http://stackoverflow.com/questions/12647471/the-truth-value-of-an-array-with-more-than-one-element-is-ambigous-when-trying-t). Have a look at the answers there – YXD Jan 31 '14 at 16:51

1 Answers1

2

I'm not certain that I understand what you're trying to do, but you might want ((a < 0) & (b > 0)).sum()

>>> a
array([-1,  0,  2,  0])
>>> b
array([4, 0, 5, 3])
>>> a < 0
array([ True, False, False, False], dtype=bool)
>>> b > 0
array([ True, False,  True,  True], dtype=bool)
>>> ((a < 0) & (b > 0)).sum()
1
Mike Graham
  • 73,987
  • 14
  • 101
  • 130