<?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: Puzzler: Intermediate sums</title>
	<atom:link href="http://blogs.mathworks.com/videos/2008/12/16/puzzler-intermediate-sums/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/videos/2008/12/16/puzzler-intermediate-sums/</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: Daniel</title>
		<link>http://blogs.mathworks.com/videos/2008/12/16/puzzler-intermediate-sums/#comment-1274</link>
		<dc:creator>Daniel</dc:creator>
		<pubDate>Fri, 19 Dec 2008 10:55:35 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2008/12/16/puzzler-intermediate-sums/#comment-1274</guid>
		<description>&lt;pre&gt; &lt;code&gt;
clear all;
clc;

a =...
[1 2 3
1 2 3
1 2 3
3 2 1
2 1 3
1 3 2]

k=3 % sum over k rows

[l,c]=size(a);

ind=cumsum(repmat([1 k-1],1,l/k)) % contains line indices
                                  % to sum up over
                                  % [form_1 to_1 from_2 to_2 ...]

mysum=@(n) sum(a(ind(n):ind(n+1),:)); % take indices ind and sum from
                                      % one to the next in the list
       
j=1:2:2*l/k; % take one odd ones

b=zeros(l+numel(j),c); % preallocate b
ind2=~mod(1:size(b,1),k+1)&#039;; % where to put the sum lines
b(~ind2,:)=a; % where to put the rest
b(ind2,:)=cell2mat(arrayfun(mysum,j,&#039;UniformOutput&#039;,false)&#039;) % build the sums
&lt;/code&gt; &lt;/pre&gt;

I didn&#039;t even think about the reshape command... :-(</description>
		<content:encoded><![CDATA[<pre> <code>
clear all;
clc;

a =...
[1 2 3
1 2 3
1 2 3
3 2 1
2 1 3
1 3 2]

k=3 % sum over k rows

[l,c]=size(a);

ind=cumsum(repmat([1 k-1],1,l/k)) % contains line indices
                                  % to sum up over
                                  % [form_1 to_1 from_2 to_2 ...]

mysum=@(n) sum(a(ind(n):ind(n+1),:)); % take indices ind and sum from
                                      % one to the next in the list

j=1:2:2*l/k; % take one odd ones

b=zeros(l+numel(j),c); % preallocate b
ind2=~mod(1:size(b,1),k+1)'; % where to put the sum lines
b(~ind2,:)=a; % where to put the rest
b(ind2,:)=cell2mat(arrayfun(mysum,j,'UniformOutput',false)') % build the sums
</code> </pre>
<p>I didn&#8217;t even think about the reshape command&#8230; :-(</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dhull</title>
		<link>http://blogs.mathworks.com/videos/2008/12/16/puzzler-intermediate-sums/#comment-1269</link>
		<dc:creator>dhull</dc:creator>
		<pubDate>Wed, 17 Dec 2008 18:40:19 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2008/12/16/puzzler-intermediate-sums/#comment-1269</guid>
		<description>@Daniel,

I like your solution.  Let me tell you why.

You use a test case that is built right into the code.  You could comment it out later so that is available not only for testing, but also for documenting the function.

You do one distinct operation per line.  When it comes time to debug, you would be able to watch each step to understand what is going on.

You use the &quot;readable functions&quot; (cat instead of [b; c], you could read your code out loud and understand it better)

You explicitly call out the direction of summing and concatenation so there is no doubt what is going on.

While some of your variable names are not implicitly descriptive, they do show an orderly progression of intermediate results.

Variable names that do deserve descriptive names have them.

This is solid code that anyone, including you in a week, will be able to understand and maintain.  I think this is very important.

Thank you,
Doug</description>
		<content:encoded><![CDATA[<p>@Daniel,</p>
<p>I like your solution.  Let me tell you why.</p>
<p>You use a test case that is built right into the code.  You could comment it out later so that is available not only for testing, but also for documenting the function.</p>
<p>You do one distinct operation per line.  When it comes time to debug, you would be able to watch each step to understand what is going on.</p>
<p>You use the &#8220;readable functions&#8221; (cat instead of [b; c], you could read your code out loud and understand it better)</p>
<p>You explicitly call out the direction of summing and concatenation so there is no doubt what is going on.</p>
<p>While some of your variable names are not implicitly descriptive, they do show an orderly progression of intermediate results.</p>
<p>Variable names that do deserve descriptive names have them.</p>
<p>This is solid code that anyone, including you in a week, will be able to understand and maintain.  I think this is very important.</p>
<p>Thank you,<br />
Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Armyr</title>
		<link>http://blogs.mathworks.com/videos/2008/12/16/puzzler-intermediate-sums/#comment-1268</link>
		<dc:creator>Daniel Armyr</dc:creator>
		<pubDate>Wed, 17 Dec 2008 08:54:04 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2008/12/16/puzzler-intermediate-sums/#comment-1268</guid>
		<description>Oh yeah, kudos to Darren R who sees to have taken into account input that does not have a length which is an even multiple of 3.</description>
		<content:encoded><![CDATA[<p>Oh yeah, kudos to Darren R who sees to have taken into account input that does not have a length which is an even multiple of 3.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Armyr</title>
		<link>http://blogs.mathworks.com/videos/2008/12/16/puzzler-intermediate-sums/#comment-1267</link>
		<dc:creator>Daniel Armyr</dc:creator>
		<pubDate>Wed, 17 Dec 2008 08:51:52 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2008/12/16/puzzler-intermediate-sums/#comment-1267</guid>
		<description>Funny, I was solving a very similar problem just yesterday. I also went straight for the version jos did, although I have a preference for doing one step per line instead of long one-liners. I also did some extra reshaping in order to maintain controll.

&lt;pre&gt; &lt;code&gt;
%% Reshape challenge
a = [1 2 3; 1 2 3; 1 2 3; 3 2 1; 2 1 3; 1 3 2];
check = [1 2 3; 1 2 3; 1 2 3; 3 6 9; 3 2 1; 2 1 3; 1 3 2; 6 6 6];
b = reshape( a&#039;, 3, 3, [] ); % Turn into a multidimensional matrix for easy handling
c = sum(b, 2); %Calculate sum of each segment.
d = cat( 2, b, c ); %Stick them together.
e = reshape ( d, 3, [] )&#039;; %Revert to original shape.
% Check that the answer is correct.
verify = ( e == check ); 
disp ( all(verify(:)) )
&lt;/code&gt; &lt;/pre&gt;

I would like to take this oppertunity to bring up a large reason for confusion when I work with Matlab. When reshaping matrices, I find it very difficult to remember in what way I need to transpose the data first and then in what order I need to set my size aguments and finally how I have to permute the result to get the data in the format I want. The fact that the ordering of coordinates (row/column or column/row) is not perfectly consistent doesn&#039;t really help. Maybe this is a fundamental difficulty of matrix problems, but I feel that it should be possible to either make a tool that helps or write up a simple non-nonsens cheat-sheet for these kinds of problems.

Just my €0.02.

--DA</description>
		<content:encoded><![CDATA[<p>Funny, I was solving a very similar problem just yesterday. I also went straight for the version jos did, although I have a preference for doing one step per line instead of long one-liners. I also did some extra reshaping in order to maintain controll.</p>
<pre> <code>
%% Reshape challenge
a = [1 2 3; 1 2 3; 1 2 3; 3 2 1; 2 1 3; 1 3 2];
check = [1 2 3; 1 2 3; 1 2 3; 3 6 9; 3 2 1; 2 1 3; 1 3 2; 6 6 6];
b = reshape( a', 3, 3, [] ); % Turn into a multidimensional matrix for easy handling
c = sum(b, 2); %Calculate sum of each segment.
d = cat( 2, b, c ); %Stick them together.
e = reshape ( d, 3, [] )'; %Revert to original shape.
% Check that the answer is correct.
verify = ( e == check );
disp ( all(verify(:)) )
</code> </pre>
<p>I would like to take this oppertunity to bring up a large reason for confusion when I work with Matlab. When reshaping matrices, I find it very difficult to remember in what way I need to transpose the data first and then in what order I need to set my size aguments and finally how I have to permute the result to get the data in the format I want. The fact that the ordering of coordinates (row/column or column/row) is not perfectly consistent doesn&#8217;t really help. Maybe this is a fundamental difficulty of matrix problems, but I feel that it should be possible to either make a tool that helps or write up a simple non-nonsens cheat-sheet for these kinds of problems.</p>
<p>Just my €0.02.</p>
<p>&#8211;DA</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Darren R</title>
		<link>http://blogs.mathworks.com/videos/2008/12/16/puzzler-intermediate-sums/#comment-1266</link>
		<dc:creator>Darren R</dc:creator>
		<pubDate>Wed, 17 Dec 2008 01:43:56 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2008/12/16/puzzler-intermediate-sums/#comment-1266</guid>
		<description>This is how I would do it. There may be bugs because I wasn&#039;t able to test it.

&lt;pre&gt; &lt;code&gt;
b = puzzler(a)

[Nrows,Ncols] = size(a);
newrows = floor(Nrows/3);
b = zeros(Nrows+newrows,Ncols);

for n=1:newrows
    b(4*n-3:4*n-1,:) = a(3*n-2:3*n,:);
    b(4*n,:) = sum(a(3*n-2:3*n,:));
end

if (Nrows/3 != newrows)
    b(4*n+1:Nrows+newrows,:) = a(3*newrows+1:Nrows,:);
end

&lt;/code&gt; &lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>This is how I would do it. There may be bugs because I wasn&#8217;t able to test it.</p>
<pre> <code>
b = puzzler(a)

[Nrows,Ncols] = size(a);
newrows = floor(Nrows/3);
b = zeros(Nrows+newrows,Ncols);

for n=1:newrows
    b(4*n-3:4*n-1,:) = a(3*n-2:3*n,:);
    b(4*n,:) = sum(a(3*n-2:3*n,:));
end

if (Nrows/3 != newrows)
    b(4*n+1:Nrows+newrows,:) = a(3*newrows+1:Nrows,:);
end

</code> </pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jos</title>
		<link>http://blogs.mathworks.com/videos/2008/12/16/puzzler-intermediate-sums/#comment-1265</link>
		<dc:creator>Jos</dc:creator>
		<pubDate>Tue, 16 Dec 2008 15:43:49 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2008/12/16/puzzler-intermediate-sums/#comment-1265</guid>
		<description>One solution could be:

&lt;pre&gt; &lt;code&gt;
% reshape the 3xN-by-M array into a 3-by-N*M array
b = reshape(a,3,[]) ; 
% concatenate the sum over each 3 elements
b = [b ; sum(b)] ;
% reshape into a (4xN)-by-M array
b = reshape(b,[],size(a,2)) ; 
&lt;/code&gt; &lt;/pre&gt;

or in one line

&lt;pre&gt; &lt;code&gt;
% add proper comments ...
b = reshape([reshape(a,3,[]) ; sum(reshape(a,3,[]))],[],3)
&lt;/code&gt; &lt;/pre&gt;

Jos</description>
		<content:encoded><![CDATA[<p>One solution could be:</p>
<pre> <code>
% reshape the 3xN-by-M array into a 3-by-N*M array
b = reshape(a,3,[]) ;
% concatenate the sum over each 3 elements
b = [b ; sum(b)] ;
% reshape into a (4xN)-by-M array
b = reshape(b,[],size(a,2)) ;
</code> </pre>
<p>or in one line</p>
<pre> <code>
% add proper comments ...
b = reshape([reshape(a,3,[]) ; sum(reshape(a,3,[]))],[],3)
</code> </pre>
<p>Jos</p>
]]></content:encoded>
	</item>
</channel>
</rss>

