September 12th, 2013, 07:31 AM
Help with numpy error: ambiguous truth value
I have this operation on numpy arrays
I thought this would return me the indexes where the elements in all the three arrays is greater than zero.
non_zero_idx = (target_std > 0) and (warped_std > 0) and (mask.data > 0)
However, I get the following error.
I googled the error but was unable to understand exactly what the problem is. I was wondering if someone has any idea on how to resolve this.
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Thanks a lot for any help you can give me.
September 12th, 2013, 08:49 AM
This puzzled me too, first time I saw this ambiguity message.
Do you want
array((0,1)) and array((1,1))
to evaluate to
array((0,1)) or to True?
You probably want:
A = numpy.array((0,1,2,3))
print((1<A) * (A%2))
The complete table of dyadic Boolean operations written as common operators <, *, GCD, etceteras is shown here. It also appears in, as I recall, the gtk operations for combining bit maps.
non_zero_idx = ((target_std > 0) *(warped_std > 0) *(mask.data))
Last edited by b49P23TIvg; September 12th, 2013 at 08:53 AM.
Reason: remove unneeded assignment.
[/code] are essential for python code and Makefiles!