[Numpy-discussion] Using logical function on more than 2 arrays, availability of a "between" function ?
Eric Firing
efiring@hawaii....
Sun Mar 25 13:33:30 CDT 2012
On 03/25/2012 06:55 AM, Pierre Haessig wrote:
> Hi,
>
> I have an off topic but somehow related question :
>
> Le 19/03/2012 12:04, Matthieu Rigal a écrit :
>> array = numpy.logical_and(numpy.logical_and(aBlueChannel< 1.0, aNirChannel>
>> (aBlueChannel * 1.0)), aNirChannel< (aBlueChannel * 1.8))
> Is there any significant difference between :
>
> z = np.logical_and(x,y) and
> z= x& y (assuming x and y are already numpy arrays and not just list)
>
> I've always used the& (and | and ~) operator because it's of course
> much shorter ;-)
>
> I've seen no mention of the "&" operator in np.logical_and docstring so
> I wonder...
There is a big difference: &, |, and ~ are bitwise operators, not
logical operators, so they work like logical operators only if operating
on booleans (or at least arrays containing nothing but integer zeros and
ones) and only if you bear in mind that & and | have lower precedence
than their logical counterparts. Therefore you often need to use more
parentheses than you might have expected.
In [1]: a = np.array([1])
In [2]: b = np.array([2])
In [5]: np.logical_and(a,b)
Out[5]: array([ True], dtype=bool)
In [6]: a & b
Out[6]: array([0])
Using the bitwise operators in place of logical operators is a hack to
get around limitations of the language; but, if done carefully, it is a
useful one.
Eric
>
> Best,
> Pierre
>
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
More information about the NumPy-Discussion
mailing list