[Numpy-discussion] Complex 'where' expression
Robert Kern
robert.kern@gmail....
Tue Mar 27 13:59:11 CDT 2007
Ludwig wrote:
> In pseudo code:
>
> indexes = where(image1 > 0 and image2 > 0 and sigma < quality)
> result[indexes] = i # scalar
>
>
> When I run this numpy tells me that the that the truth value of the array
> comparison is ambiguous -- but I do not want to compare the whole arrays here,
> but the value for every point.
The Python keywords "and", "or", and "not" try to evaluate the truth value of
the objects. We cannot override them to operate elementwise. Instead, the
operators &, |, and ~, respectively, are the elementwise operators on arrays of
booleans. Thus, you want this:
indexes = where((image1>0) & (image2>0) & (sigma<quality))
However, also note that numpy arrays support indexing with boolean arrays, so
the where() is superfluous.
mask = (image1>0) & (image2>0) & (sigma<quality)
result[mask] = i
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Numpy-discussion
mailing list