<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Empty Arrays with Flow of Control and Logical Operators</title>
	<atom:link href="http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Mon, 13 Feb 2012 13:24:10 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-32083</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 07 Mar 2011 12:07:39 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-32083</guid>
		<description>Sundar-

isnan is an elementwise function, so it&#039;s returning arrays the same size and as x and y, not scalars, which is why &amp;&amp; is complaining.  You may want to judiciously use the function any with ~isnan and collapse the right dimensions to get a scalar before doing the &amp;&amp;.

--Loren</description>
		<content:encoded><![CDATA[<p>Sundar-</p>
<p>isnan is an elementwise function, so it&#8217;s returning arrays the same size and as x and y, not scalars, which is why &#038;&#038; is complaining.  You may want to judiciously use the function any with ~isnan and collapse the right dimensions to get a scalar before doing the &#038;&#038;.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sundar</title>
		<link>http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-32082</link>
		<dc:creator>Sundar</dc:creator>
		<pubDate>Sat, 05 Mar 2011 11:47:00 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-32082</guid>
		<description>Hi
    I have a problem with using these short-circuit operators.
Say, x = NaN; x(:,:,2) = NaN; x(:,:,3) = NaN;
        y = 1; y(:,:,2) = 2; y(:,:,3) = 3;
if isnan(x(:,:,:)) == 0 &amp;&amp; isnan(y(:,:,:)) == 0
   % Execute functions
end

I want to execute certain functions only if I have at least one non-NaN value in x and at least one NaN value in y. This means that the loop should end in the above example because all values of x is NaN.
But I get this error: &#039;Operands to the &#124;&#124; and &amp;&amp; operators must be convertible to logical scalar values&#039;

Am I doing something wrong here?

Thanks</description>
		<content:encoded><![CDATA[<p>Hi<br />
    I have a problem with using these short-circuit operators.<br />
Say, x = NaN; x(:,:,2) = NaN; x(:,:,3) = NaN;<br />
        y = 1; y(:,:,2) = 2; y(:,:,3) = 3;<br />
if isnan(x(:,:,:)) == 0 &amp;&amp; isnan(y(:,:,:)) == 0<br />
   % Execute functions<br />
end</p>
<p>I want to execute certain functions only if I have at least one non-NaN value in x and at least one NaN value in y. This means that the loop should end in the above example because all values of x is NaN.<br />
But I get this error: &#8216;Operands to the || and &amp;&amp; operators must be convertible to logical scalar values&#8217;</p>
<p>Am I doing something wrong here?</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-31489</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 09 Jul 2010 21:59:00 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-31489</guid>
		<description>Vinz-

You might try dbstop if error so you can see the link of code and the values where the issue is exposed.

If that doesn&#039;t help, please contact technical support (link on right of blog) with enough information to reproduce the issue so they can help you. 

--Loren</description>
		<content:encoded><![CDATA[<p>Vinz-</p>
<p>You might try dbstop if error so you can see the link of code and the values where the issue is exposed.</p>
<p>If that doesn&#8217;t help, please contact technical support (link on right of blog) with enough information to reproduce the issue so they can help you. </p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: vinz</title>
		<link>http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-31481</link>
		<dc:creator>vinz</dc:creator>
		<pubDate>Thu, 08 Jul 2010 19:38:23 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-31481</guid>
		<description>I am unable to resolve the below error. I am unable to figure out where the problem is becuase; Iam not using any &#124;&#124; and &amp;&amp;  in my program nor combinations of if and while statements. 
ERROR: 
??? Operands to the &#124;&#124; and &amp;&amp; operators must be convertible to logical scalar values.

Error in ==&gt; fzero at 333
    elseif ~isfinite(fx) &#124;&#124; ~isreal(fx)

Error in ==&gt; BRALHX_Condenser_Equations at 69
   Fdel=fzero(Fun,1.4766e-007,options);

It will really helpful if I understand more about this error with respect to fzero function.

Cheers
Vinz</description>
		<content:encoded><![CDATA[<p>I am unable to resolve the below error. I am unable to figure out where the problem is becuase; Iam not using any || and &amp;&amp;  in my program nor combinations of if and while statements.<br />
ERROR:<br />
??? Operands to the || and &amp;&amp; operators must be convertible to logical scalar values.</p>
<p>Error in ==&gt; fzero at 333<br />
    elseif ~isfinite(fx) || ~isreal(fx)</p>
<p>Error in ==&gt; BRALHX_Condenser_Equations at 69<br />
   Fdel=fzero(Fun,1.4766e-007,options);</p>
<p>It will really helpful if I understand more about this error with respect to fzero function.</p>
<p>Cheers<br />
Vinz</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: LyVe</title>
		<link>http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30845</link>
		<dc:creator>LyVe</dc:creator>
		<pubDate>Mon, 23 Nov 2009 13:02:15 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30845</guid>
		<description>This behavior has tripped me up quite a few times now.

Although the rules make sense individually, they are inconsistent*.

What trips me up is when I use an if statement and add a condition later. So I start with
&lt;pre&gt;
if A then B
&lt;/pre&gt;
and then decide that C is a condition too, so I turn it into
&lt;pre&gt;
if A &amp; C then B
&lt;/pre&gt;
only to have things crash if A is empty - which was previously not a problem.

I find it counterintuitive that adding code concerning C changes the way A is treated. This makes error-checking a lot more difficult, until you learn to take countermeasures.

I appreciate being able to use empty as false: if the variable has no value, it definitely doesn&#039;t have the right one. This is very intuitive for me. However, I&#039;m now considering teaching myself not to use this shortcut (and adding a short-circuited ~isempty instead), because it might trip me up later, unless I can learn to remember that when adding a condition to an if or while, I also need to add a short-circuited isempty... 

I&#039;d really like for the short-circuiting operators to evaluate in the same way that if and while do; not just for empties but also for arrays and such. Is there a particular reason that they require scalar values? Could we get &#124;&#124;&#124; and &amp;&amp;&amp; (for instance) to short-circuit for arrays (including empties)?

* I must admit that it&#039;s quite probable that there is no consistent way to handle this, other than never accepting [] where a logical is expected. This would be more robust for the user (smaller chance of a small change causing large problems), but less convenient. So maybe all of this simply can&#039;t be helped.</description>
		<content:encoded><![CDATA[<p>This behavior has tripped me up quite a few times now.</p>
<p>Although the rules make sense individually, they are inconsistent*.</p>
<p>What trips me up is when I use an if statement and add a condition later. So I start with</p>
<pre>
if A then B
</pre>
<p>and then decide that C is a condition too, so I turn it into</p>
<pre>
if A &amp; C then B
</pre>
<p>only to have things crash if A is empty &#8211; which was previously not a problem.</p>
<p>I find it counterintuitive that adding code concerning C changes the way A is treated. This makes error-checking a lot more difficult, until you learn to take countermeasures.</p>
<p>I appreciate being able to use empty as false: if the variable has no value, it definitely doesn&#8217;t have the right one. This is very intuitive for me. However, I&#8217;m now considering teaching myself not to use this shortcut (and adding a short-circuited ~isempty instead), because it might trip me up later, unless I can learn to remember that when adding a condition to an if or while, I also need to add a short-circuited isempty&#8230; </p>
<p>I&#8217;d really like for the short-circuiting operators to evaluate in the same way that if and while do; not just for empties but also for arrays and such. Is there a particular reason that they require scalar values? Could we get ||| and &amp;&amp;&amp; (for instance) to short-circuit for arrays (including empties)?</p>
<p>* I must admit that it&#8217;s quite probable that there is no consistent way to handle this, other than never accepting [] where a logical is expected. This would be more robust for the user (smaller chance of a small change causing large problems), but less convenient. So maybe all of this simply can&#8217;t be helped.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30819</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Thu, 19 Nov 2009 11:52:44 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30819</guid>
		<description>Marcel-

Please feel free to use the link at the right to make this enhancement suggestion.  I personally don&#039;t mind instead using another try-catch inside the catch block.  I find the nesting clarifies my thinking.

--Loren</description>
		<content:encoded><![CDATA[<p>Marcel-</p>
<p>Please feel free to use the link at the right to make this enhancement suggestion.  I personally don&#8217;t mind instead using another try-catch inside the catch block.  I find the nesting clarifies my thinking.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marcel Zwiers</title>
		<link>http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30817</link>
		<dc:creator>Marcel Zwiers</dc:creator>
		<pubDate>Thu, 19 Nov 2009 10:13:40 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30817</guid>
		<description>It&#039;s not exactly on topic but what I miss in the try-catch syntax is a flow as in the if-elseif-end-syntax. Thus I would like to see a try-elsetry-catch block. I think it&#039;s a very natural thing to have, no?

Best,
Marcel</description>
		<content:encoded><![CDATA[<p>It&#8217;s not exactly on topic but what I miss in the try-catch syntax is a flow as in the if-elseif-end-syntax. Thus I would like to see a try-elsetry-catch block. I think it&#8217;s a very natural thing to have, no?</p>
<p>Best,<br />
Marcel</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30793</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 16 Nov 2009 13:38:27 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30793</guid>
		<description>Ben 

if {} errors because the expression has to evaluate to something numeric, even if empty.  Same for i.  Neither one can be converted to a logical array.

--Loren</description>
		<content:encoded><![CDATA[<p>Ben </p>
<p>if {} errors because the expression has to evaluate to something numeric, even if empty.  Same for i.  Neither one can be converted to a logical array.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30792</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 16 Nov 2009 13:34:33 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30792</guid>
		<description>Ben-

Another good way to look at the behavior!  Thanks.

--Loren</description>
		<content:encoded><![CDATA[<p>Ben-</p>
<p>Another good way to look at the behavior!  Thanks.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30791</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Mon, 16 Nov 2009 08:03:29 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30791</guid>
		<description>Matt/Loren,

Defining all([])=true has the additional advantage that all([A;B])=(all(A)&amp;all(B)) holds when either array is empty.

The older versions used to generate an error for &quot;if []&quot; didn&#039;t they?  (although &quot;if nan&quot;, &quot;if {}&quot; and &quot;if i&quot; still do!)  I guess that why I still prefer to write &quot;if ~isempty(x) &amp;&amp; all(x(:))&quot; when it&#039;s needed.</description>
		<content:encoded><![CDATA[<p>Matt/Loren,</p>
<p>Defining all([])=true has the additional advantage that all([A;B])=(all(A)&amp;all(B)) holds when either array is empty.</p>
<p>The older versions used to generate an error for &#8220;if []&#8221; didn&#8217;t they?  (although &#8220;if nan&#8221;, &#8220;if {}&#8221; and &#8220;if i&#8221; still do!)  I guess that why I still prefer to write &#8220;if ~isempty(x) &amp;&amp; all(x(:))&#8221; when it&#8217;s needed.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

