[Numpy-discussion] Complex 'where' expression

Tom Denniston tom.denniston@alum.dartmouth....
Tue Mar 27 18:59:41 CDT 2007


You need to use numpy.logical_and.

The and and or operators do not work elementwise on arrays to the best
of my understanding. I think it is a limitation on the way logical
operators are applied in python or maybe it is an intentional numpy
limitation.  I'm sure others on the list could explain better.

Also where is the wrong function.  You want compress:

In [8]: image2 = numpy.array([-1,1,1,1])

In [9]: image1 = numpy.array([1,-1,2,3])

In [10]: numpy.arange(len(image2)).compress(numpy.logical_and(image1 >
0, image2 > 0))
Out[10]: array([2, 3])

Others might have a better way but this at least works.

--Tom

On 3/27/07, Ludwig <ludwigbrinckmann@gmail.com> wrote:
> A bit new to numpy, trying to move over some IDL code.
>
> I have a complex array selection expression, where I need an array of indexes as
> a result, so I can use this as a selection expression to apply changes to a
> different array.
>
> I have two images, image1 and image2, plus an array sigma, the result of a
> previous calculation. All have the same shape. quality is a scalar threshold.
>
> I need the indexes where image1 and image2 are not 0 and the sigma value at that
> point is lower than my threshold. I then take these indices and store some value
> against this in a different array.
>
> 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.
>
> How do I do this
>
> Regards
>
> Ludwig
>
>
>
>
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>


More information about the Numpy-discussion mailing list