#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    13
    Rep Power
    0

    Help with numpy error: ambiguous truth value


    Hello,

    I have this operation on numpy arrays

    Code:
    non_zero_idx = (target_std > 0) and (warped_std > 0) and (mask.data > 0)
    I thought this would return me the indexes where the elements in all the three arrays is greater than zero.

    However, I get the following error.
    Code:
    ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
    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.

    Thanks a lot for any help you can give me.

    Luca
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    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:

    import numpy
    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]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo