<?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: Basics: Code review- the thought process in rewriting code for clarity</title>
	<atom:link href="http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/</link>
	<description>Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.</description>
	<lastBuildDate>Fri, 10 Feb 2012 20:31:50 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: dhull</title>
		<link>http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1786</link>
		<dc:creator>dhull</dc:creator>
		<pubDate>Fri, 22 Jan 2010 18:57:19 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1786</guid>
		<description>@Jon,

That looks well documented and formatted!

Thanks
Doug</description>
		<content:encoded><![CDATA[<p>@Jon,</p>
<p>That looks well documented and formatted!</p>
<p>Thanks<br />
Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jon</title>
		<link>http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1782</link>
		<dc:creator>jon</dc:creator>
		<pubDate>Wed, 20 Jan 2010 13:23:57 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1782</guid>
		<description>I would propose the following alternative code:
&lt;pre&gt;
function B = interleavedTranspose(A)
%INTERLEAVEDTRANSPOSE Interleaves alternate rows of input matrix and
%transposes to produce output matrix
%   % we wish to rearrange the input matrix as follows:
% 1) interleave every other row with the row above it
% 2) transpose the result
% So for example if we start with the matrix
% [11 12 13 ;
%  21 22 23;
%  31 32 33;
%  41 42 43]
% would first interleave every other row with the row above it to get
%
% [11 21 12 22 13 23;
%  31 41 32 42 33 43]
%
% and then transpose to get:
%
% [11 31;
%  21 41;
%  12 32;
%  22 42;
%  13 33;
%  23 43]
%
% We will do this by first separating alternate rows (odd and even row
% numbers into two matrices
% oddRows = [11 12 13 ;
%            31 32 33]
% evenRows = [21 22 23;
%             41 42 43]
% then make a new matrix to hold the result
% B = [ 0 0 0 0 0 0 ;
%       0 0 0 0 0 0 ]
%
% place the odd rows into new matrix leaving spaces to interleave even rows
% B = [ 11 0 12 0 13 0;
%       31 0 32 0 33 0]
% interleave the even rows
% B = [ 11 21 12 22 13 23 ;
%       31 41 32 42 33 43]
% transpose
% B = B&#039;


% input matrix must have even number of rows

% get dimensions of input matrix for later use
[numRows,numCols] = size(A);

% first check that input matrix has even number of rows
if rem(numRows,2)~= 0
    error(&#039;input matrix must have even number of rows&#039;)
end

% separate alternate rows and put into two matrices
oddRows = A(1:2:numRows,:);
evenRows = A(2:2:numRows,:);

% make new matrix to hold interleaved rows
B = zeros(numRows/2,numCols*2)

% place odd rows into new matrix leaving spaces to interleave even rows
B(:,1:2:end) = oddRows;
%  interleave even rows
B(:,2:2:end) = evenRows;

% transpose to get final result
B = B&#039;

end

&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I would propose the following alternative code:</p>
<pre>
function B = interleavedTranspose(A)
%INTERLEAVEDTRANSPOSE Interleaves alternate rows of input matrix and
%transposes to produce output matrix
%   % we wish to rearrange the input matrix as follows:
% 1) interleave every other row with the row above it
% 2) transpose the result
% So for example if we start with the matrix
% [11 12 13 ;
%  21 22 23;
%  31 32 33;
%  41 42 43]
% would first interleave every other row with the row above it to get
%
% [11 21 12 22 13 23;
%  31 41 32 42 33 43]
%
% and then transpose to get:
%
% [11 31;
%  21 41;
%  12 32;
%  22 42;
%  13 33;
%  23 43]
%
% We will do this by first separating alternate rows (odd and even row
% numbers into two matrices
% oddRows = [11 12 13 ;
%            31 32 33]
% evenRows = [21 22 23;
%             41 42 43]
% then make a new matrix to hold the result
% B = [ 0 0 0 0 0 0 ;
%       0 0 0 0 0 0 ]
%
% place the odd rows into new matrix leaving spaces to interleave even rows
% B = [ 11 0 12 0 13 0;
%       31 0 32 0 33 0]
% interleave the even rows
% B = [ 11 21 12 22 13 23 ;
%       31 41 32 42 33 43]
% transpose
% B = B'

% input matrix must have even number of rows

% get dimensions of input matrix for later use
[numRows,numCols] = size(A);

% first check that input matrix has even number of rows
if rem(numRows,2)~= 0
    error('input matrix must have even number of rows')
end

% separate alternate rows and put into two matrices
oddRows = A(1:2:numRows,:);
evenRows = A(2:2:numRows,:);

% make new matrix to hold interleaved rows
B = zeros(numRows/2,numCols*2)

% place odd rows into new matrix leaving spaces to interleave even rows
B(:,1:2:end) = oddRows;
%  interleave even rows
B(:,2:2:end) = evenRows;

% transpose to get final result
B = B'

end
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: jon</title>
		<link>http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1781</link>
		<dc:creator>jon</dc:creator>
		<pubDate>Tue, 19 Jan 2010 18:59:18 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1781</guid>
		<description>For many years the Mathworks promoted the idea of eliminating loops (vectorization) whenever possible, both for efficiency and also because in many cases the matrix operations were clearer. For an extreme example it is much easier to see what B = A&#039; means then to interpret an equivalent set of nested loops. 
Yes I agree that brevity isn&#039;t the end all, but I think that your use of loops here where they really aren&#039;t needed is too extreme and tends to miss the point of using a powerful matrix based language rather than coding in a more primitive language where everything has to be done in loops.</description>
		<content:encoded><![CDATA[<p>For many years the Mathworks promoted the idea of eliminating loops (vectorization) whenever possible, both for efficiency and also because in many cases the matrix operations were clearer. For an extreme example it is much easier to see what B = A&#8217; means then to interpret an equivalent set of nested loops.<br />
Yes I agree that brevity isn&#8217;t the end all, but I think that your use of loops here where they really aren&#8217;t needed is too extreme and tends to miss the point of using a powerful matrix based language rather than coding in a more primitive language where everything has to be done in loops.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dhull</title>
		<link>http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1723</link>
		<dc:creator>dhull</dc:creator>
		<pubDate>Wed, 09 Dec 2009 17:52:01 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1723</guid>
		<description>@Ludvik,

Thank you for your comments.  You make good points, there are trade-offs.  I tend to work on smaller datasets, and am mostly concerned about readability.  It sounds like you have other metrics to satisfy, so you use other techniques.

Thanks,
Doug</description>
		<content:encoded><![CDATA[<p>@Ludvik,</p>
<p>Thank you for your comments.  You make good points, there are trade-offs.  I tend to work on smaller datasets, and am mostly concerned about readability.  It sounds like you have other metrics to satisfy, so you use other techniques.</p>
<p>Thanks,<br />
Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: LudvikL</title>
		<link>http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1721</link>
		<dc:creator>LudvikL</dc:creator>
		<pubDate>Wed, 09 Dec 2009 12:50:36 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1721</guid>
		<description>Hi,

I&#039;d think that all the original code needs is a replacement of &quot;shiftdim&quot; with &quot;permute&quot;. That gets you rid of the &quot;shiftdata&quot; function, since shiftdim can only circulate dimensions; permute is more general:

&lt;pre&gt;
b1=magic(6)
b2=reshape(b1,2,3,6)
b3=permute(b2,[1 3 2])
b4=reshape(b3,12,3)
&lt;/pre&gt;

I like the idea of expansion into higher dimensions, I use it a lot. Loops are fine, but can be tedious for high-dimensional problems. If one prints out b2 (; left out), it becomes quite clear that the result is there already -- all that needs to be done is reshuffling of dimensions for the next reshape. Also such code (hopefully) operates on indexes, rather than data and checks the dimensions for you, to some extend. 

I would normally do this on one line to save memory. There are pros and cons, of course. When the array becomes too large, one might think of segmenting it and saving intermediate results within a loop.

Thanks,
Ludvik</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I&#8217;d think that all the original code needs is a replacement of &#8220;shiftdim&#8221; with &#8220;permute&#8221;. That gets you rid of the &#8220;shiftdata&#8221; function, since shiftdim can only circulate dimensions; permute is more general:</p>
<pre>
b1=magic(6)
b2=reshape(b1,2,3,6)
b3=permute(b2,[1 3 2])
b4=reshape(b3,12,3)
</pre>
<p>I like the idea of expansion into higher dimensions, I use it a lot. Loops are fine, but can be tedious for high-dimensional problems. If one prints out b2 (; left out), it becomes quite clear that the result is there already &#8212; all that needs to be done is reshuffling of dimensions for the next reshape. Also such code (hopefully) operates on indexes, rather than data and checks the dimensions for you, to some extend. </p>
<p>I would normally do this on one line to save memory. There are pros and cons, of course. When the array becomes too large, one might think of segmenting it and saving intermediate results within a loop.</p>
<p>Thanks,<br />
Ludvik</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bruce</title>
		<link>http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1685</link>
		<dc:creator>Bruce</dc:creator>
		<pubDate>Thu, 19 Nov 2009 20:31:01 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1685</guid>
		<description>I&#039;ve been using Matlab for a long time, but never thought of coding a test matrix like you did, e.g.,
1 2 3 4 ...
11 12 13 14 ...
.
.
.

Great idea for checking how code manipulates data, in stages</description>
		<content:encoded><![CDATA[<p>I&#8217;ve been using Matlab for a long time, but never thought of coding a test matrix like you did, e.g.,<br />
1 2 3 4 &#8230;<br />
11 12 13 14 &#8230;<br />
.<br />
.<br />
.</p>
<p>Great idea for checking how code manipulates data, in stages</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dhull</title>
		<link>http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1552</link>
		<dc:creator>dhull</dc:creator>
		<pubDate>Mon, 31 Aug 2009 15:40:29 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1552</guid>
		<description>Ola,

http://www.mathworks.com/support can answer all your questions.

Thanks,
Doug</description>
		<content:encoded><![CDATA[<p>Ola,</p>
<p><a href="http://www.mathworks.com/support" rel="nofollow">http://www.mathworks.com/support</a> can answer all your questions.</p>
<p>Thanks,<br />
Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ola</title>
		<link>http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1547</link>
		<dc:creator>Ola</dc:creator>
		<pubDate>Fri, 28 Aug 2009 11:33:51 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1547</guid>
		<description>Hi, I&#039;m new to this blog, and as a matter of fact I&#039;m lost so I was kinda hoping someone will point to me the right link to post my questions about SimEvent tool box.

Thanks a lot in advance.</description>
		<content:encoded><![CDATA[<p>Hi, I&#8217;m new to this blog, and as a matter of fact I&#8217;m lost so I was kinda hoping someone will point to me the right link to post my questions about SimEvent tool box.</p>
<p>Thanks a lot in advance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: matt fig</title>
		<link>http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1546</link>
		<dc:creator>matt fig</dc:creator>
		<pubDate>Thu, 27 Aug 2009 15:12:44 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1546</guid>
		<description>Daniel Armyr,

I suspect the reason why MLint doesn&#039;t warn about parenthesis is that they invoke no function call.  Parenthesis are only used to group in this context.  Brackets on the other hand, are used to concatenate.  If there is only one array inside the brackets, there is nothing to concatenate, yet the internal concatenate function is called anyway!  This is an inefficiency.</description>
		<content:encoded><![CDATA[<p>Daniel Armyr,</p>
<p>I suspect the reason why MLint doesn&#8217;t warn about parenthesis is that they invoke no function call.  Parenthesis are only used to group in this context.  Brackets on the other hand, are used to concatenate.  If there is only one array inside the brackets, there is nothing to concatenate, yet the internal concatenate function is called anyway!  This is an inefficiency.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Armyr</title>
		<link>http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1544</link>
		<dc:creator>Daniel Armyr</dc:creator>
		<pubDate>Thu, 27 Aug 2009 07:23:25 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/08/21/basics-code-review-the-thought-process-in-rewriting-code-for-clarity/#comment-1544</guid>
		<description>@dhull
Yes, but so are an extra pair of parenthesis, but sometimes you leave them in there anyways and you do not see matlab complaining. But that is a discussion to be had in a service request, not here.</description>
		<content:encoded><![CDATA[<p>@dhull<br />
Yes, but so are an extra pair of parenthesis, but sometimes you leave them in there anyways and you do not see matlab complaining. But that is a discussion to be had in a service request, not here.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

