compound conditions with pylab.find

Is it possible to use multiple conditionals with the pylab.find() function?

For example, in Matlab, I can do the following:

x = rand(1,10);
i = find(x > 0.5 & x < 0.9); %returns the indices between 0.5 and 0.9

In Python: (ipython -pylab)
x = rand(1,10)

None of the following approaches work:
#i = find(x > 0.5 and x < 0.9)
#i = find(x > 0.5 && x < 0.9)
#i = find(x > 0.5 & x < 0.9)

I'm pretty certain there is some other way to do this using a numpy method, and if that's all there is, then I will happily use it.

However, if the goal of the pylab module is to achieve (some level of) compatibility with Matlab, then may I suggest this sort of functionality be added to the pylab.find() function?

Thanks,

Mike

Michael Hearne wrote:

Is it possible to use multiple conditionals with the pylab.find() function?

For example, in Matlab, I can do the following:

x = rand(1,10);
i = find(x > 0.5 & x < 0.9); %returns the indices between 0.5 and 0.9

In Python: (ipython -pylab)
x = rand(1,10)

None of the following approaches work:
#i = find(x > 0.5 and x < 0.9)
#i = find(x > 0.5 && x < 0.9)
#i = find(x > 0.5 & x < 0.9)

I'm pretty certain there is some other way to do this using a numpy method, and if that's all there is, then I will happily use it.

However, if the goal of the pylab module is to achieve (some level of) compatibility with Matlab, then may I suggest this sort of functionality be added to the pylab.find() function?

Thanks,

Mike
  
Mike: Use numpy.logical_and/numpy.logical_or for elementwise logical operations. In your case,

find(logical_and(x>0.5,x<0.9))

works, as does using parantheses

find((x > 0.5) & (x < 0.9))

-Jeff

Michael Hearne wrote:

Is it possible to use multiple conditionals with the pylab.find() function?

For example, in Matlab, I can do the following:

x = rand(1,10);
i = find(x > 0.5 & x < 0.9); %returns the indices between 0.5 and 0.9

In Python: (ipython -pylab)
x = rand(1,10)

None of the following approaches work:
#i = find(x > 0.5 and x < 0.9)
#i = find(x > 0.5 && x < 0.9)
#i = find(x > 0.5 & x < 0.9)

I'm pretty certain there is some other way to do this using a numpy method, and if that's all there is, then I will happily use it.

However, if the goal of the pylab module is to achieve (some level of) compatibility with Matlab, then may I suggest this sort of functionality be added to the pylab.find() function?

This is a fundamental python/numpy problem. The workaround is

x = rand(10)
i = find((x > 0.5) & (x < 0.9))

Note the explicit parentheses to make the conditionals evaluated before the bitwise and. The real problem is that we are stuck using bitwise and as a surrogate for logical and. This problem is not going away any time soon, although proposed solutions have been discussed.

Also note that in numpy we are using a 1-D array. If you really want the 2-D array, then you will have to index it as x[:,i].

Longer term, the recommendation is that you move away from trying to write matlab code using pylab, and instead use numpy idioms directly.

import numpy as np
x = np.random.rand(10)
y = x[(x > 0.5) & (x < 0.9)]

or if you really want the indices:

i = np.nonzero((x > 0.5) & (x < 0.9))[0]

Eric

ยทยทยท

Thanks,

Mike

------------------------------------------------------------------------------
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options