<?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: Afterthoughts on implicit expansion	</title>
	<atom:link href="https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=afterthoughts-on-implicit-expansion</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Fri, 04 Aug 2017 01:37:22 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.3</generator>
	<item>
		<title>
		By: Sue Ann Koay		</title>
		<link>https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-411438</link>

		<dc:creator><![CDATA[Sue Ann Koay]]></dc:creator>
		<pubDate>Fri, 04 Aug 2017 01:37:22 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6750#comment-411438</guid>

					<description><![CDATA[Having only recently installed Matlab 2017a, I almost immediately &quot;discovered&quot; this feature and am compelled to say that implicit expansion gives me the creeps. As an outside example: why do some languages like C++ impose type safety? It&#039;s not because programmers can&#039;t write their own type checking code. For a 10-line function I can easily write 50 lines of code to perform every possible check I can think of to ensure that the inputs are of the correct type and sizes and ranges and whatnot. Will this make me a better coder? Maybe. Will chances be high that I&#039;ll do it for every function? If I existed solely for such a purpose, maybe. Will this help others read and modify my code down the line? Well...

As a person from a joint CS/science background, I&#039;m certainly not advocating sloppy coding practices. I think implicit expansion (and yes also lack of type safety) actually goes &lt;i&gt;towards&lt;/i&gt; encouraging sloppy coding practices. Now instead of a self-documenting piece of code where one uses &lt;code&gt;bsxfun()&lt;/code&gt;, if I read the line &lt;code&gt;a .* b&lt;/code&gt; somewhere I have to wonder if it is the outer product of vectors or an element-wise product or anything goes. The best designed systems limit the amount of mistakes that humans can make, and allowing the syntax itself to be sloppy doesn&#039;t seem very encouraging.]]></description>
			<content:encoded><![CDATA[<p>Having only recently installed Matlab 2017a, I almost immediately &#8220;discovered&#8221; this feature and am compelled to say that implicit expansion gives me the creeps. As an outside example: why do some languages like C++ impose type safety? It&#8217;s not because programmers can&#8217;t write their own type checking code. For a 10-line function I can easily write 50 lines of code to perform every possible check I can think of to ensure that the inputs are of the correct type and sizes and ranges and whatnot. Will this make me a better coder? Maybe. Will chances be high that I&#8217;ll do it for every function? If I existed solely for such a purpose, maybe. Will this help others read and modify my code down the line? Well&#8230;</p>
<p>As a person from a joint CS/science background, I&#8217;m certainly not advocating sloppy coding practices. I think implicit expansion (and yes also lack of type safety) actually goes <i>towards</i> encouraging sloppy coding practices. Now instead of a self-documenting piece of code where one uses <code>bsxfun()</code>, if I read the line <code>a .* b</code> somewhere I have to wonder if it is the outer product of vectors or an element-wise product or anything goes. The best designed systems limit the amount of mistakes that humans can make, and allowing the syntax itself to be sloppy doesn&#8217;t seem very encouraging.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Marshall		</title>
		<link>https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-395929</link>

		<dc:creator><![CDATA[Marshall]]></dc:creator>
		<pubDate>Mon, 12 Dec 2016 19:04:04 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6750#comment-395929</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-394957&quot;&gt;David B&lt;/a&gt;.

@DavidB if we know we are receiving vectors, and they may be oriented differently, the following would be more elegant compared to any requiring any logical branches:

&lt;pre lang=&quot;matlab&quot;&gt;
dataA(:)+dataB(:);
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-394957">David B</a>.</p>
<p>@DavidB if we know we are receiving vectors, and they may be oriented differently, the following would be more elegant compared to any requiring any logical branches:</p>
<pre lang="matlab">
dataA(:)+dataB(:);
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-395128</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Sat, 03 Dec 2016 17:37:57 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6750#comment-395128</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-395033&quot;&gt;Guillaume&lt;/a&gt;.

@DavidB + @Guillaume + @TheBlackCat - if I remember correctly, my client wanted the code to continue processing only when the 2 inputs were both vectors, although possibly of different dimensionality. So, [1,2,3] should be combinable with [3;4;5] but not with [3;4] (which would error out downstream). In such cases the try-catch block gives the expected results, but &lt;code&gt;a(:)+b(:)&lt;/code&gt; would not have. 

As I said in the post, the code snippet is just a simplified version and the actual coding details don&#039;t really matter. The important thing in my opinion is that for this specific use-case, a functional change in Matlab R2016b caused fully-legitimate code to return different results, and since the functional change was not documented as having a compatibility aspect, this caused an operational problem that was unacceptable. In this respect, it really does not matter whether the client&#039;s code was due to bad coding or to explicit design.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-395033">Guillaume</a>.</p>
<p>@DavidB + @Guillaume + @TheBlackCat &#8211; if I remember correctly, my client wanted the code to continue processing only when the 2 inputs were both vectors, although possibly of different dimensionality. So, [1,2,3] should be combinable with [3;4;5] but not with [3;4] (which would error out downstream). In such cases the try-catch block gives the expected results, but <code>a(:)+b(:)</code> would not have. </p>
<p>As I said in the post, the code snippet is just a simplified version and the actual coding details don&#8217;t really matter. The important thing in my opinion is that for this specific use-case, a functional change in Matlab R2016b caused fully-legitimate code to return different results, and since the functional change was not documented as having a compatibility aspect, this caused an operational problem that was unacceptable. In this respect, it really does not matter whether the client&#8217;s code was due to bad coding or to explicit design.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: David B		</title>
		<link>https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-395084</link>

		<dc:creator><![CDATA[David B]]></dc:creator>
		<pubDate>Sat, 03 Dec 2016 10:33:39 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6750#comment-395084</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-394957&quot;&gt;David B&lt;/a&gt;.

@TheBlackCat From the limited information we have available we are assuming the data is a vector. If that is the case then I think something like this code snippet would work nicely and is perfectly human readable.

&lt;pre lang=&quot;matlab&quot;&gt;
if isrow(a) &amp;&amp; ~isrow(b) &#124;&#124; iscolumn(a) &amp;&amp; ~iscolumn(b)
    b = b&#039;;
end
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-394957">David B</a>.</p>
<p>@TheBlackCat From the limited information we have available we are assuming the data is a vector. If that is the case then I think something like this code snippet would work nicely and is perfectly human readable.</p>
<pre lang="matlab">
if isrow(a) &#038;& ~isrow(b) || iscolumn(a) &#038;& ~iscolumn(b)
    b = b';
end
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: TheBlackCat		</title>
		<link>https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-395034</link>

		<dc:creator><![CDATA[TheBlackCat]]></dc:creator>
		<pubDate>Fri, 02 Dec 2016 17:19:45 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6750#comment-395034</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-395033&quot;&gt;Guillaume&lt;/a&gt;.

It is hard to say without seeing more code.  It may very well be that the data can only come in a few formats, so transposing it is the correct thing to do.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-395033">Guillaume</a>.</p>
<p>It is hard to say without seeing more code.  It may very well be that the data can only come in a few formats, so transposing it is the correct thing to do.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Guillaume		</title>
		<link>https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-395033</link>

		<dc:creator><![CDATA[Guillaume]]></dc:creator>
		<pubDate>Fri, 02 Dec 2016 16:05:07 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6750#comment-395033</guid>

					<description><![CDATA[Well, presumably, the code is operating on vectors, so the alternative could be 

  &lt;pre lang=&quot;matlab&quot;&gt;
   dataA(:) + dataB(:)
  &lt;/pre&gt;

But really, the proper alternative would have been to find out why the data does not come with the expected shape rather than take a gamble and flip it. I&#039;m with David B, the code is a strong indication that something is very wrong in the algorithm somewhere and that one day, given some particular input, it&#039;s going to break in even more unpleasant ways.]]></description>
			<content:encoded><![CDATA[<p>Well, presumably, the code is operating on vectors, so the alternative could be </p>
<pre lang="matlab">
   dataA(:) + dataB(:)
  </pre>
<p>But really, the proper alternative would have been to find out why the data does not come with the expected shape rather than take a gamble and flip it. I&#8217;m with David B, the code is a strong indication that something is very wrong in the algorithm somewhere and that one day, given some particular input, it&#8217;s going to break in even more unpleasant ways.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: TheBlackCat		</title>
		<link>https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-394968</link>

		<dc:creator><![CDATA[TheBlackCat]]></dc:creator>
		<pubDate>Thu, 01 Dec 2016 22:28:38 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6750#comment-394968</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-394957&quot;&gt;David B&lt;/a&gt;.

What would your alternative be?  The equivalent one I can think of would be:

&lt;pre lang=&quot;matlab&quot;&gt;
if numel(dataA)~=1 &amp;&amp; numel(dataB)~=1 &amp;&amp; any(size(dataA)~=size(dataB))
    dataB = dataB&#039;;
end
&lt;/pre&gt;

A more strict test would be:

&lt;pre lang=&quot;matlab&quot;&gt;
if numel(dataA)~=1 &amp;&amp; numel(dataB)~=1 &amp;&amp; ndim(dataA)==2 &amp;&amp; ndim(dataB)==2 &amp;&amp; any(size(dataA)~=size(dataB)) &amp;&amp; all(fliplr(size(dataA))==size(dataB))
    dataB = dataB&#039;;
end
&lt;/pre&gt;

So yes, probably from a code correctness standpoint you are probably right. However, from a readability and maintainability standpoint their solution is pretty elegant.  Will it be slower? Yes, but in many cases not enough to make a difference. Does it have corner cases that it doesn&#039;t handle? Yes, although those may not be relevant or may get caught later. But even without a comment I can tell in an instant what their code is doing, while it would take me some time to figure out what either of the two examples I posted did.  

If you have another approach that is as simple and easy-to-read as the above case then of course I will retract that. But otherwise, the best algorithm from a CS standpoint isn&#039;t necessarily the best approach once you have to start involving humans and want to be able to figure out what your code is doing 3 years down the road.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-394957">David B</a>.</p>
<p>What would your alternative be?  The equivalent one I can think of would be:</p>
<pre lang="matlab">
if numel(dataA)~=1 &#038;& numel(dataB)~=1 &#038;& any(size(dataA)~=size(dataB))
    dataB = dataB';
end
</pre>
<p>A more strict test would be:</p>
<pre lang="matlab">
if numel(dataA)~=1 &#038;& numel(dataB)~=1 &#038;& ndim(dataA)==2 &#038;& ndim(dataB)==2 &#038;& any(size(dataA)~=size(dataB)) &#038;& all(fliplr(size(dataA))==size(dataB))
    dataB = dataB';
end
</pre>
<p>So yes, probably from a code correctness standpoint you are probably right. However, from a readability and maintainability standpoint their solution is pretty elegant.  Will it be slower? Yes, but in many cases not enough to make a difference. Does it have corner cases that it doesn&#8217;t handle? Yes, although those may not be relevant or may get caught later. But even without a comment I can tell in an instant what their code is doing, while it would take me some time to figure out what either of the two examples I posted did.  </p>
<p>If you have another approach that is as simple and easy-to-read as the above case then of course I will retract that. But otherwise, the best algorithm from a CS standpoint isn&#8217;t necessarily the best approach once you have to start involving humans and want to be able to figure out what your code is doing 3 years down the road.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-394959</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Thu, 01 Dec 2016 20:36:24 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6750#comment-394959</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-394957&quot;&gt;David B&lt;/a&gt;.

@David - while I fully agree with you that it&#039;s not good coding style/practice, I&#039;ve seen much worse client codes. Most Matlab users don&#039;t have a degree in computer science, and sadly enough even CS grads often exhibit deplorable coding. In fact, my personal experience has been that only a minority of Matlab users have high-quality code. Most Matlab users use Matlab as an engineering tool and not as an end to itself: as long as something works, they don&#039;t mind if it&#039;s nice-looking - they just move on to solving the next problem. In this sense, the snippet above is beautiful in its simplicity, and to hell with the CS purists...]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-394957">David B</a>.</p>
<p>@David &#8211; while I fully agree with you that it&#8217;s not good coding style/practice, I&#8217;ve seen much worse client codes. Most Matlab users don&#8217;t have a degree in computer science, and sadly enough even CS grads often exhibit deplorable coding. In fact, my personal experience has been that only a minority of Matlab users have high-quality code. Most Matlab users use Matlab as an engineering tool and not as an end to itself: as long as something works, they don&#8217;t mind if it&#8217;s nice-looking &#8211; they just move on to solving the next problem. In this sense, the snippet above is beautiful in its simplicity, and to hell with the CS purists&#8230;</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: David B		</title>
		<link>https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-394957</link>

		<dc:creator><![CDATA[David B]]></dc:creator>
		<pubDate>Thu, 01 Dec 2016 20:20:47 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6750#comment-394957</guid>

					<description><![CDATA[If nothing else I hope that the exercise of debugging this proved as a rude wake-up call for the author of that original and terrible code in the try/catch. They should be ashamed.]]></description>
			<content:encoded><![CDATA[<p>If nothing else I hope that the exercise of debugging this proved as a rude wake-up call for the author of that original and terrible code in the try/catch. They should be ashamed.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: TheBlackCat		</title>
		<link>https://undocumentedmatlab.com/articles/afterthoughts-on-implicit-expansion#comment-394854</link>

		<dc:creator><![CDATA[TheBlackCat]]></dc:creator>
		<pubDate>Thu, 01 Dec 2016 00:30:54 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6750#comment-394854</guid>

					<description><![CDATA[Your client is lucky the problem got caught.  A lot of such issues will likely go unnoticed.  For example, basically anything with a mathematical operation followed by the use of linear indexing (such as in a for loop) will seem to work fine but will give mathematically incorrect results.  I know you ideally shouldn&#039;t be doing this, and I assume MATLAB internal code doesn&#039;t do it very much, but I see code like that all the time from people with less of a programming background, and sometimes your algorithm requires it.

I had always assumed the reason MATLAB hadn&#039;t implemented this feature over the last 15 years or so was that it was too big of a backwards-compatibility break.]]></description>
			<content:encoded><![CDATA[<p>Your client is lucky the problem got caught.  A lot of such issues will likely go unnoticed.  For example, basically anything with a mathematical operation followed by the use of linear indexing (such as in a for loop) will seem to work fine but will give mathematically incorrect results.  I know you ideally shouldn&#8217;t be doing this, and I assume MATLAB internal code doesn&#8217;t do it very much, but I see code like that all the time from people with less of a programming background, and sometimes your algorithm requires it.</p>
<p>I had always assumed the reason MATLAB hadn&#8217;t implemented this feature over the last 15 years or so was that it was too big of a backwards-compatibility break.</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
