<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Bottom Line on Logical</title>
	<link>http://blogs.mathworks.com/loren/2006/11/02/bottom-line-on-logical/</link>
	<description>Loren Shure  works on design of the MATLAB language at The MathWorks. She writes here about once a week on MATLAB programming and related topics. </description>
	<pubDate>Fri, 29 Aug 2008 03:12:18 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: Oliver A. Chapman, PE</title>
		<link>http://blogs.mathworks.com/loren/2006/11/02/bottom-line-on-logical/#comment-14265</link>
		<dc:creator>Oliver A. Chapman, PE</dc:creator>
		<pubDate>Mon, 06 Nov 2006 17:42:41 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/11/02/bottom-line-on-logical/#comment-14265</guid>
		<description>Steve &#38; Loren,

Certainly it is fair for you to defend the way MatLab operates.  And, you are correct regarding the documentation of the "if" statement.

Even though I continue to lobby MatLab for specific improvements in the documentation when I encounter deficiencies, the documentation for "if" is very good.

However, for well designed software, the documentation serves as a backstop to its logical &#38; reasonable operation.  Further, in this case, the documentation of the "or" operator doesn't give any hint to its different behavior when imbedded in an "if" statement.

I stand by the point I made that none of us here think it is reasonable for the outcome for an "or" operation be order dependent.

Further, I am more troubled by Steve's comments regarding the specification for the "or" statement.  A reasonable answer would be something like, "Yes MatLab's specification for the "or" statement does discuss this behavior, but for proprietary reasons, we can't share it with you."  Instead, he glosses over the issue with a "non-denial-denial."

In my first reply to this, on November 1, I assumed there was a performance benefit to this feature and asked for an example.  No reply yet.  Show us how these other customers have taken advantage of this feature.

Reading between the lines, I suspect that this behavior kind of "happened" in the original version of MatLab and has recently been incorporated into the current specifications.

Here is the bottom line:  MatLab is an expensive bit of software that is well worth the price.  It is generally very well designed and has outstanding documentation.  Users can quickly engage with MatLab even when approaching it from varied backgrounds and perspectives.

However, for the users like me and those around me, this feature that we've been discussing is not expected and Loren's column is a good forum to discuss it.  I read her column just so I can learn these details.  Give some examples as I discuss above and then we'll move on to other topics.</description>
		<content:encoded><![CDATA[<p>Steve &amp; Loren,</p>
<p>Certainly it is fair for you to defend the way MatLab operates.  And, you are correct regarding the documentation of the &#8220;if&#8221; statement.</p>
<p>Even though I continue to lobby MatLab for specific improvements in the documentation when I encounter deficiencies, the documentation for &#8220;if&#8221; is very good.</p>
<p>However, for well designed software, the documentation serves as a backstop to its logical &amp; reasonable operation.  Further, in this case, the documentation of the &#8220;or&#8221; operator doesn&#8217;t give any hint to its different behavior when imbedded in an &#8220;if&#8221; statement.</p>
<p>I stand by the point I made that none of us here think it is reasonable for the outcome for an &#8220;or&#8221; operation be order dependent.</p>
<p>Further, I am more troubled by Steve&#8217;s comments regarding the specification for the &#8220;or&#8221; statement.  A reasonable answer would be something like, &#8220;Yes MatLab&#8217;s specification for the &#8220;or&#8221; statement does discuss this behavior, but for proprietary reasons, we can&#8217;t share it with you.&#8221;  Instead, he glosses over the issue with a &#8220;non-denial-denial.&#8221;</p>
<p>In my first reply to this, on November 1, I assumed there was a performance benefit to this feature and asked for an example.  No reply yet.  Show us how these other customers have taken advantage of this feature.</p>
<p>Reading between the lines, I suspect that this behavior kind of &#8220;happened&#8221; in the original version of MatLab and has recently been incorporated into the current specifications.</p>
<p>Here is the bottom line:  MatLab is an expensive bit of software that is well worth the price.  It is generally very well designed and has outstanding documentation.  Users can quickly engage with MatLab even when approaching it from varied backgrounds and perspectives.</p>
<p>However, for the users like me and those around me, this feature that we&#8217;ve been discussing is not expected and Loren&#8217;s column is a good forum to discuss it.  I read her column just so I can learn these details.  Give some examples as I discuss above and then we&#8217;ll move on to other topics.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Eddins</title>
		<link>http://blogs.mathworks.com/loren/2006/11/02/bottom-line-on-logical/#comment-14177</link>
		<dc:creator>Steve Eddins</dc:creator>
		<pubDate>Sat, 04 Nov 2006 02:06:35 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/11/02/bottom-line-on-logical/#comment-14177</guid>
		<description>Oliver - I understand your consternation about this behavior.  Believe me, you are not alone.  But there's no reason for your suggestion of a lack of a spec.  In fact, the behavior is completely documented. (See Documentation -&#62; MATLAB -&#62; MATLAB Programming -&#62; Logical Operators -&#62; Short-Circuiting in Element-Wise Operators.)

We cannot change this behavior now without breaking a great deal of existing customer code.  So what we have done instead is to introduce the scalar, always-short-circuiting logical operators, and to encourage their use in if and while conditionals.  The MATLAB Editor will even make this suggestion to you if you use "&#124;" or "&#38;" in an if or while conditional expression.</description>
		<content:encoded><![CDATA[<p>Oliver - I understand your consternation about this behavior.  Believe me, you are not alone.  But there&#8217;s no reason for your suggestion of a lack of a spec.  In fact, the behavior is completely documented. (See Documentation -&gt; MATLAB -&gt; MATLAB Programming -&gt; Logical Operators -&gt; Short-Circuiting in Element-Wise Operators.)</p>
<p>We cannot change this behavior now without breaking a great deal of existing customer code.  So what we have done instead is to introduce the scalar, always-short-circuiting logical operators, and to encourage their use in if and while conditionals.  The MATLAB Editor will even make this suggestion to you if you use &#8220;|&#8221; or &#8220;&amp;&#8221; in an if or while conditional expression.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oliver A. Chapman, PE</title>
		<link>http://blogs.mathworks.com/loren/2006/11/02/bottom-line-on-logical/#comment-14176</link>
		<dc:creator>Oliver A. Chapman, PE</dc:creator>
		<pubDate>Sat, 04 Nov 2006 01:06:06 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/11/02/bottom-line-on-logical/#comment-14176</guid>
		<description>Loren,

I think that MatLab has made this sufficiently complex.

Let's go back to the "or" operator.

&#62;&#62; []&#124;4

ans =

     []

&#62;&#62; 4&#124;[]

ans =

     []

As expected, I get the empty result, regardless of order of the inputs.

But, there is no rational justification for the different result if imbedded in an if or while statement.

&#62;&#62; if []&#124;4
disp('true');
else
disp('false');
end;
false


&#62;&#62; if 4&#124;[]
disp('true');
else
disp('false');
end;
true
&#62;&#62;

What does the specification for the MatLab "if" statement say regarding this feature?

Or, was the "if" statement written without a specification?</description>
		<content:encoded><![CDATA[<p>Loren,</p>
<p>I think that MatLab has made this sufficiently complex.</p>
<p>Let&#8217;s go back to the &#8220;or&#8221; operator.</p>
<p>&gt;&gt; []|4</p>
<p>ans =</p>
<p>     []</p>
<p>&gt;&gt; 4|[]</p>
<p>ans =</p>
<p>     []</p>
<p>As expected, I get the empty result, regardless of order of the inputs.</p>
<p>But, there is no rational justification for the different result if imbedded in an if or while statement.</p>
<p>&gt;&gt; if []|4<br />
disp(&#8217;true&#8217;);<br />
else<br />
disp(&#8217;false&#8217;);<br />
end;<br />
false</p>
<p>&gt;&gt; if 4|[]<br />
disp(&#8217;true&#8217;);<br />
else<br />
disp(&#8217;false&#8217;);<br />
end;<br />
true<br />
&gt;&gt;</p>
<p>What does the specification for the MatLab &#8220;if&#8221; statement say regarding this feature?</p>
<p>Or, was the &#8220;if&#8221; statement written without a specification?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/11/02/bottom-line-on-logical/#comment-14171</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 03 Nov 2006 20:34:59 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/11/02/bottom-line-on-logical/#comment-14171</guid>
		<description>Oliver-

You are missing the part about scalar expansion that works with many operators in MATLAB, including +, &#124;, &#038;.  If I say
&lt;pre&gt;
x = 1:3
y = []
x+y
&lt;/pre&gt;
I get the error message about matrix dimensions must agree, but I don't if x = 3.  In that case, MATLAB does a scalar expansion  and I get the scalar to expand to the size 0x0 and therefore, I get an empty result.

The same thing is happening with 
&lt;pre&gt;
if [] &#124; 4
  whatever
end
&lt;/pre&gt;

MATLAB can't tell from the first part of the expression if the entire expression is true yet, so it keeps going.  Then MATLAB computes [] &#124; 4, using scalar expansion, empty is returned as the final expression for the &lt;kbd&gt;if&lt;/kbd&gt; statement, and the result is then treated as &lt;kbd&gt;false&lt;/kbd&gt;.

--Loren</description>
		<content:encoded><![CDATA[<p>Oliver-</p>
<p>You are missing the part about scalar expansion that works with many operators in MATLAB, including +, |, &#038;.  If I say</p>
<pre>
x = 1:3
y = []
x+y
</pre>
<p>I get the error message about matrix dimensions must agree, but I don&#8217;t if x = 3.  In that case, MATLAB does a scalar expansion  and I get the scalar to expand to the size 0&#215;0 and therefore, I get an empty result.</p>
<p>The same thing is happening with </p>
<pre>
if [] | 4
  whatever
end
</pre>
<p>MATLAB can&#8217;t tell from the first part of the expression if the entire expression is true yet, so it keeps going.  Then MATLAB computes [] | 4, using scalar expansion, empty is returned as the final expression for the <kbd>if</kbd> statement, and the result is then treated as <kbd>false</kbd>.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oliver A. Chapman, PE</title>
		<link>http://blogs.mathworks.com/loren/2006/11/02/bottom-line-on-logical/#comment-14168</link>
		<dc:creator>Oliver A. Chapman, PE</dc:creator>
		<pubDate>Fri, 03 Nov 2006 17:33:29 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/11/02/bottom-line-on-logical/#comment-14168</guid>
		<description>A few of us looked at this for a while yesterday and we think the short circuiting in the "or" operator isn't operating properly.  Or, at least isn't operating as we expect.

Here are some commands and comments to illustrate:

First, rationally, the "or" operator should operate on logical variables.  But, clearly it is an advantage if MatLab can convert from other data types to logical within the arguments of the operator.  So, a question we asked was, how does MatLab convert doubles to logicals?

logical(4)

ans =

     1

This makes sense and is what we expect.

Now a slightly harder situation, converting an empty double to logical.

&#62;&#62; logical([])

ans =

     []

&#62;&#62; isempty([])

ans =

     1

This makes sense, an empty double gets converted to an empty logical.

Now, how does the "or" operator deal with different sizes of variables:

&#62;&#62; (1:5)&#124;(1:4)
??? Error using ==&#62; or
Inputs must have the same size.

How about if one is empty?

&#62;&#62; (1:5)&#124;([])
??? Error using ==&#62; or
Inputs must have the same size.

That makes sense because the empty variable has size zero.

&#62;&#62; size([])

ans =

     0     0

With this background, the expected response within the "if" statement would be the same error message.

Further, it certainly doesn't make sense for the "or" operator to short circuit to false when the first variable is false.  Although this makes sense for an "and" operator, doesn't the "or" operator always have to test both?</description>
		<content:encoded><![CDATA[<p>A few of us looked at this for a while yesterday and we think the short circuiting in the &#8220;or&#8221; operator isn&#8217;t operating properly.  Or, at least isn&#8217;t operating as we expect.</p>
<p>Here are some commands and comments to illustrate:</p>
<p>First, rationally, the &#8220;or&#8221; operator should operate on logical variables.  But, clearly it is an advantage if MatLab can convert from other data types to logical within the arguments of the operator.  So, a question we asked was, how does MatLab convert doubles to logicals?</p>
<p>logical(4)</p>
<p>ans =</p>
<p>     1</p>
<p>This makes sense and is what we expect.</p>
<p>Now a slightly harder situation, converting an empty double to logical.</p>
<p>&gt;&gt; logical([])</p>
<p>ans =</p>
<p>     []</p>
<p>&gt;&gt; isempty([])</p>
<p>ans =</p>
<p>     1</p>
<p>This makes sense, an empty double gets converted to an empty logical.</p>
<p>Now, how does the &#8220;or&#8221; operator deal with different sizes of variables:</p>
<p>&gt;&gt; (1:5)|(1:4)<br />
??? Error using ==&gt; or<br />
Inputs must have the same size.</p>
<p>How about if one is empty?</p>
<p>&gt;&gt; (1:5)|([])<br />
??? Error using ==&gt; or<br />
Inputs must have the same size.</p>
<p>That makes sense because the empty variable has size zero.</p>
<p>&gt;&gt; size([])</p>
<p>ans =</p>
<p>     0     0</p>
<p>With this background, the expected response within the &#8220;if&#8221; statement would be the same error message.</p>
<p>Further, it certainly doesn&#8217;t make sense for the &#8220;or&#8221; operator to short circuit to false when the first variable is false.  Although this makes sense for an &#8220;and&#8221; operator, doesn&#8217;t the &#8220;or&#8221; operator always have to test both?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/11/02/bottom-line-on-logical/#comment-14163</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 03 Nov 2006 14:24:22 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/11/02/bottom-line-on-logical/#comment-14163</guid>
		<description>Dan-

Thanks for pointing this out.  I was thinking about &lt;kbd&gt;if&lt;/kbd&gt; and &lt;kbd&gt;while&lt;/kbd&gt; so I went back and made that more explicit.

--Loren</description>
		<content:encoded><![CDATA[<p>Dan-</p>
<p>Thanks for pointing this out.  I was thinking about <kbd>if</kbd> and <kbd>while</kbd> so I went back and made that more explicit.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan K</title>
		<link>http://blogs.mathworks.com/loren/2006/11/02/bottom-line-on-logical/#comment-14144</link>
		<dc:creator>Dan K</dc:creator>
		<pubDate>Thu, 02 Nov 2006 21:48:03 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/11/02/bottom-line-on-logical/#comment-14144</guid>
		<description>Um, that seems a little to generalized of a statement to me.  Using &#38; and &#124; is the only way to do logical indexing, isn't it?  I often use those tests for cases such as:

data=rand(100,1);
middleHalf=data(data&#62;0.25 &#38; data</description>
		<content:encoded><![CDATA[<p>Um, that seems a little to generalized of a statement to me.  Using &amp; and | is the only way to do logical indexing, isn&#8217;t it?  I often use those tests for cases such as:</p>
<p>data=rand(100,1);<br />
middleHalf=data(data&gt;0.25 &amp; data</p>
]]></content:encoded>
	</item>
</channel>
</rss>
