[SciPy-user] Newbie question about boolean condition argument

Otto Tronarp otto at tronarp.se
Wed Dec 6 06:26:18 CST 2006


Quoting Giorgio Luciano <giorgio.luciano at chimica.unige.it>:
[...]
>
> here they are (matlab code)
>
> s2=find(sig(:,in:c)>0.001&sig(:,in:c)<=0.01)
>   if s2~=[]; % for Matlab 7 use if not(isempty(s2));
>
> the first problem is that the command find (and also where) does not
> support a logicla operator as condition :(
> because
>
> s2=find(sig(:,in:c)>0.001) works..
> but s2=find(sig(:,in:c)>0.001&sig(:,in:c)<=0.01)
>
> give me an error like this
>
> Traceback (most recent call last):
>   File "<pyshell#66>", line 1, in -toplevel-
>     s3=find(sig1[:,arange(ini,c)]>0.001 and sig1[:,arange(ini,c)]<=0.00 )
> ValueError: The truth value of an array with more than one element is
> ambiguous. Use a.any() or a.all()
>
> I've read all the manual about difference in logical and bitwise
> operation... tried with different parenthese, using AND or logical_and,
> & etc. but I didn' manage to do it.

Are you sure that you used logical_and in the right way? It seems to  
work in my simple example.

In [10]:x = rand(20)

In [11]:logical_and(x > 0.01,  x < 0.5)
Out[11]:NumPy array, format: long
[True True False True True False True True True False False False False
  False True False True True False True]

In [12]:where(logical_and(x > 0.01,  x < 0.5))
Out[12]:NumPy array, format: long
[ 0  1  3  4  6  7  8 14 16 17 19]

/Otto




More information about the SciPy-user mailing list