equivalent to isempty command in matlab (newbie question)
Today I've also posted a question to scipy groups because I've thought I've found a solution but this work good bar(N, b1[:,0], width, color='r', yerr=binterv) ############ s3=find(sig1[:,arange(ini,c)]<=0.001) b1=b.flatten() #if s3!=[]: for i3 in arange(len(s3)): text(s3[i3], b1[s3[i3]+ini],'***') s2=find(logical_and(sig1[:,arange(ini,c)]>0.001,sig1[:,arange(ini,c)]<=0.01)) for i2 in arange(len(s2)): text(s2[i2], b1[s2[i2]+ini],'**') s1=find(logical_and(sig1[:,arange(ini,c)]>0.01,sig1[:,arange(ini,c)]<=0.05)) for i1 in arange(len(s1)): text(s1[i1], b1[s1[i1]+ini],'*') title('Plot of the coefficients of the model') and when i uncomment the ifs3!=[] part it does not... so in this case I've solve the problem.. but is there an equivalent for isempty matlab command in numpy ? Thanks in advance for the reply Giorgio
Giorgio Luciano wrote:
Today I've also posted a question to scipy groups because I've thought I've found a solution but
this work good
bar(N, b1[:,0], width, color='r', yerr=binterv) ############ s3=find(sig1[:,arange(ini,c)]<=0.001) b1=b.flatten() #if s3!=[]: for i3 in arange(len(s3)): text(s3[i3], b1[s3[i3]+ini],'***') s2=find(logical_and(sig1[:,arange(ini,c)]>0.001,sig1[:,arange(ini,c)]<=0.01)) for i2 in arange(len(s2)): text(s2[i2], b1[s2[i2]+ini],'**') s1=find(logical_and(sig1[:,arange(ini,c)]>0.01,sig1[:,arange(ini,c)]<=0.05)) for i1 in arange(len(s1)): text(s1[i1], b1[s1[i1]+ini],'*') title('Plot of the coefficients of the model')
and when i uncomment the ifs3!=[] part it does not... so in this case I've solve the problem.. but is there an equivalent for isempty matlab command in numpy ?
Use (len(s3) != 0) instead of (s3 != []).  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
On 12/6/06, Giorgio Luciano <giorgio.luciano@chimica.unige.it> wrote:
Today I've also posted a question to scipy groups because I've thought I've found a solution but
this work good
bar(N, b1[:,0], width, color='r', yerr=binterv) ############ s3=find(sig1[:,arange(ini,c)]<=0.001)
Just a few tips before I answer your question. Is sig1 a global constant? It is a good practice to write constant names in uppercase. Otherwise consider passing it as a function argument.
b1=b.flatten() #if s3!=[]:
if s3: ...
for i3 in arange(len(s3)):
Although this works, a nosurprise way is to use standard xrange: for i3 in xrange(len(s3)): ... or enumerate: for i3, elem in enumerate(s3): ...
text(s3[i3], b1[s3[i3]+ini],'***') s2=find(logical_and(sig1[:,arange(ini,c)]>0.001,sig1[:,arange(ini,c)]<=0.01))
Boolean operators are also ok, just remember about parentheses and operators priority: (sig1[:,arange(ini,c)]>0.001) & (sig1[:,arange(ini,c)]<=0.01)
for i2 in arange(len(s2)): text(s2[i2], b1[s2[i2]+ini],'**') s1=find(logical_and(sig1[:,arange(ini,c)]>0.01,sig1[:,arange(ini,c)]<=0.05)) for i1 in arange(len(s1)): text(s1[i1], b1[s1[i1]+ini],'*') title('Plot of the coefficients of the model')
and when i uncomment the ifs3!=[] part it does not...
I think you have just discovered a bug or it's an inconsistency I didn't know of?
print numpy.array([1,1]) == [], numpy.array([1,1]) != [] False True print numpy.array([1]) == [], numpy.array([1]) != [] [] [] print numpy.array([]) == [], numpy.array([]) != [] [] []
numpy.__version__ '1.0'
so in this case I've solve the problem.. but is there an equivalent for isempty matlab command in numpy ?
Just like for other Python objects: if ifs3: print "not empty" or check if .size attribute is positive. cheers, fw
Filip Wasilewski wrote:
Just like for other Python objects:
if ifs3: print "not empty"
No, that doesn't work. numpy arrays do not have a truth value. They raise an error when you try to use them in such a context.  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
On 12/6/06, Robert Kern <robert.kern@gmail.com> wrote:
Filip Wasilewski wrote:
Just like for other Python objects:
if ifs3: print "not empty"
No, that doesn't work. numpy arrays do not have a truth value. They raise an error when you try to use them in such a context.
Right! I could swear I have checked this before posting. Evidently I got bitten by this:
bool(numpy.array([])) False bool(numpy.array([1])) True bool(numpy.array([0])) False bool(numpy.array([1,1]))
Traceback (most recent call last): File "<pyshell#8>", line 1, in toplevel bool(numpy.array([1,1])) ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() So depending on the situation one can use len or size:
len(numpy.array([[],[]])) 2 numpy.array([[],[]]).size 0
And how to understand following?
print numpy.array([1,1]) == [], numpy.array([1,1]) != [] False True print `numpy.array([1]) == []`, `numpy.array([1]) != []` array([], dtype=bool) array([], dtype=bool) print bool(numpy.array([1]) == []), bool(numpy.array([1]) != []) False False
cheers, fw
participants (3)

Filip Wasilewski

Giorgio Luciano

Robert Kern