<?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: MATLAB Puzzler: Finding the two closest points</title>
	<link>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/</link>
	<description>Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.</description>
	<pubDate>Mon, 23 Nov 2009 00:02:56 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: Yi Cao</title>
		<link>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-1005</link>
		<dc:creator>Yi Cao</dc:creator>
		<pubDate>Fri, 06 Jun 2008 19:49:10 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-1005</guid>
		<description>Doug,

Personally, I like the node count because it encourages to use meaningful variable names. A variable with a name 'd' has the same node count as using name 'distance', but latter clearly is more readable.

Yi</description>
		<content:encoded><![CDATA[<p>Doug,</p>
<p>Personally, I like the node count because it encourages to use meaningful variable names. A variable with a name &#8216;d&#8217; has the same node count as using name &#8216;distance&#8217;, but latter clearly is more readable.</p>
<p>Yi</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-1004</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Fri, 06 Jun 2008 12:57:16 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-1004</guid>
		<description>Yi,

You bring up an interesting point about metrics on code.  If people were to specify metrics to judge code on, how would they be defined?

* Binary (works/does not work)
* Length (MATLAB golf)
* Node count
* Community "thumbs up/down" voting (not automatable)


Are there automatable metrics that could be used to give an idea of what code is best?  Personally, I think Golf is an interesting challenge, but I prefer readability by making variable with descriptive names on separate lines:

&lt;pre&gt;&lt;code&gt;
flagIsBig = (variable &gt; 1000);

if flagIsBig
  %Do big code
else %not flagIsBig
  %Do not big code
end
&lt;/code&gt;&lt;/pre&gt;

This takes more characters, but is very readable.  I think only community voting can clear this up.  What do you think?

-Doug</description>
		<content:encoded><![CDATA[<p>Yi,</p>
<p>You bring up an interesting point about metrics on code.  If people were to specify metrics to judge code on, how would they be defined?</p>
<p>* Binary (works/does not work)<br />
* Length (MATLAB golf)<br />
* Node count<br />
* Community &#8220;thumbs up/down&#8221; voting (not automatable)</p>
<p>Are there automatable metrics that could be used to give an idea of what code is best?  Personally, I think Golf is an interesting challenge, but I prefer readability by making variable with descriptive names on separate lines:</p>
<pre><code>
flagIsBig = (variable > 1000);

if flagIsBig
  %Do big code
else %not flagIsBig
  %Do not big code
end
</code></pre>
<p>This takes more characters, but is very readable.  I think only community voting can clear this up.  What do you think?</p>
<p>-Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yi Cao</title>
		<link>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-1003</link>
		<dc:creator>Yi Cao</dc:creator>
		<pubDate>Fri, 06 Jun 2008 08:27:00 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-1003</guid>
		<description>Ben,

Nice solution, but it call pdist twice unnecessarily. If you wish to reduce the length of code, the following is shorter but also more readable:

&lt;pre&gt;
&lt;code&gt;d = pdist([x;y]');
[i,j] = find(squareform(d)==min(d),1,'first');&lt;/code&gt;
&lt;/pre&gt;

The length of code can be checked as follows:

&lt;pre&gt;
&lt;code&gt;t = mtree('[i,j]=find(squareform(pdist([x; y]''))==min(pdist([x; y]'')),1,''first'')')&lt;/code&gt;
&lt;/pre&gt;
which gives 33 nodes.

&lt;pre&gt;
&lt;code&gt;t = mtree('d=pdist([x; y]'');[i,j]=find(squareform(d)==min(d),1,''first'')')&lt;/code&gt;
&lt;/pre&gt;
only 28 nodes.

hth
Yi</description>
		<content:encoded><![CDATA[<p>Ben,</p>
<p>Nice solution, but it call pdist twice unnecessarily. If you wish to reduce the length of code, the following is shorter but also more readable:</p>
<pre>
<code>d = pdist([x;y]');
[i,j] = find(squareform(d)==min(d),1,'first');</code>
</pre>
<p>The length of code can be checked as follows:</p>
<pre>
<code>t = mtree('[i,j]=find(squareform(pdist([x; y]''))==min(pdist([x; y]'')),1,''first'')')</code>
</pre>
<p>which gives 33 nodes.</p>
<pre>
<code>t = mtree('d=pdist([x; y]'');[i,j]=find(squareform(d)==min(d),1,''first'')')</code>
</pre>
<p>only 28 nodes.</p>
<p>hth<br />
Yi</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-1002</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Fri, 06 Jun 2008 00:55:11 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-1002</guid>
		<description>&lt;pre&gt;&lt;code&gt;[i,j]=find(squareform(pdist([x; y]'))==min(pdist([x; y]')),1,'first')&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<pre><code>[i,j]=find(squareform(pdist([x; y]'))==min(pdist([x; y]')),1,'first')</code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yi Cao</title>
		<link>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-1001</link>
		<dc:creator>Yi Cao</dc:creator>
		<pubDate>Thu, 05 Jun 2008 23:07:32 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-1001</guid>
		<description>If we consider the simplest solution, using pdist might be the solution:

&lt;pre&gt;&lt;code&gt;
n = 10;
x = rand(1,n);
y = rand(1,n);
allDist = pdist([x;y]');
[minDist,minLoc] = min(allDist);
[loc1,loc2] = find(tril(ones(n),-1));
minLoc1 = loc1(minLoc);
minLoc2 = loc2(minLoc);
&lt;/code&gt;&lt;/pre&gt;

Alternatively, we can use bsxfun to build up the distance map.

&lt;pre&gt;&lt;code&gt;
z = x + i*y;
distMap = tril(abs(bsxfun(@minus,z,z.')));
distMap(~distMap) = Inf;
[minCol,rowLoc] = min(distMap);
[minDist,colLoc] = min(minCol);
rowLoc = rowLoc(colLoc);
&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>If we consider the simplest solution, using pdist might be the solution:</p>
<pre><code>
n = 10;
x = rand(1,n);
y = rand(1,n);
allDist = pdist([x;y]');
[minDist,minLoc] = min(allDist);
[loc1,loc2] = find(tril(ones(n),-1));
minLoc1 = loc1(minLoc);
minLoc2 = loc2(minLoc);
</code></pre>
<p>Alternatively, we can use bsxfun to build up the distance map.</p>
<pre><code>
z = x + i*y;
distMap = tril(abs(bsxfun(@minus,z,z.')));
distMap(~distMap) = Inf;
[minCol,rowLoc] = min(distMap);
[minDist,colLoc] = min(minCol);
rowLoc = rowLoc(colLoc);
</code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-1000</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Thu, 05 Jun 2008 14:00:00 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-1000</guid>
		<description>@Dan: You might want to increase the 'dummy value' for dist to INF since in some cases 10 might be the smallest (not in this case, but if the code were used elsewhere with different inputs.

@Oliver:  As a best practice, I will post my solution as the first comment so it is accessible.

Doug</description>
		<content:encoded><![CDATA[<p>@Dan: You might want to increase the &#8216;dummy value&#8217; for dist to INF since in some cases 10 might be the smallest (not in this case, but if the code were used elsewhere with different inputs.</p>
<p>@Oliver:  As a best practice, I will post my solution as the first comment so it is accessible.</p>
<p>Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oliver A. Chapman, P.E.</title>
		<link>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-999</link>
		<dc:creator>Oliver A. Chapman, P.E.</dc:creator>
		<pubDate>Wed, 04 Jun 2008 22:02:18 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-999</guid>
		<description>I really like the topic.

I haven't used meshgrid or surf before so I'm learning something new.  I must be missing something since it seems like meshgrid was written to address a limitation of the syntax option of surf(X, Y, Z) when X &#38; Y are arrays.  I'm having trouble thinking of why anyone would choose to make X &#38; Y arrays instead of vectors.

The video worked very well to talk me thru the example and you did a very fine job of recording &#38; editing a very understandable lesson.  The only negative is that after I finished the video, I went to read the FRPs for meshgrid &#38; surf, but I didn't have your code to reference since the video had ended &#38; reset.</description>
		<content:encoded><![CDATA[<p>I really like the topic.</p>
<p>I haven&#8217;t used meshgrid or surf before so I&#8217;m learning something new.  I must be missing something since it seems like meshgrid was written to address a limitation of the syntax option of surf(X, Y, Z) when X &amp; Y are arrays.  I&#8217;m having trouble thinking of why anyone would choose to make X &amp; Y arrays instead of vectors.</p>
<p>The video worked very well to talk me thru the example and you did a very fine job of recording &amp; editing a very understandable lesson.  The only negative is that after I finished the video, I went to read the FRPs for meshgrid &amp; surf, but I didn&#8217;t have your code to reference since the video had ended &amp; reset.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan Brown</title>
		<link>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-998</link>
		<dc:creator>Dan Brown</dc:creator>
		<pubDate>Wed, 04 Jun 2008 13:08:14 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-998</guid>
		<description>My solution...

&lt;pre&gt;
&lt;code&gt;dist=sqrt(bsxfun(@minus,x,x').^2+bsxfun(@minus,y,y').^2);
dist(dist==0)=10;       % Cancel the main diag
[i,j]=find(dist==min(dist(:)))
plot(x,y,'.',x(i),y(i),'o')&lt;/code&gt;
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>My solution&#8230;</p>
<pre>
<code>dist=sqrt(bsxfun(@minus,x,x').^2+bsxfun(@minus,y,y').^2);
dist(dist==0)=10;       % Cancel the main diag
[i,j]=find(dist==min(dist(:)))
plot(x,y,'.',x(i),y(i),'o')</code>
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim Davis</title>
		<link>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-997</link>
		<dc:creator>Tim Davis</dc:creator>
		<pubDate>Wed, 04 Jun 2008 10:43:09 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-997</guid>
		<description>Now, the real challenge ... can anyone solve it in O(n log n) time using recursive divide-and-conquer?  How's that for a new Puzzler?

http://en.wikipedia.org/wiki/Closest_pair_of_points

:-)</description>
		<content:encoded><![CDATA[<p>Now, the real challenge &#8230; can anyone solve it in O(n log n) time using recursive divide-and-conquer?  How&#8217;s that for a new Puzzler?</p>
<p><a href="http://en.wikipedia.org/wiki/Closest_pair_of_points" rel="nofollow">http://en.wikipedia.org/wiki/Closest_pair_of_points</a></p>
<p>:-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim Davis</title>
		<link>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-996</link>
		<dc:creator>Tim Davis</dc:creator>
		<pubDate>Wed, 04 Jun 2008 10:38:47 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-996</guid>
		<description>p.s.  when you all post code snippets, you should use the "pre" and "code" format.  It makes the code easier to read.  Maybe Doug could edit the above posts and add the code and pre's.

&#60;pre&#62;&#60;code&#62;
&lt;pre&gt;&lt;code&gt;
code goes here.
&lt;/code&gt;&lt;/pre&gt;
&#60;/pre&#62;&#60;/code&#62;

[edit by Doug to have both &#60;pre&#62; and &#60;code&#62; tags in suggestion.  Also edited old comment to do this.</description>
		<content:encoded><![CDATA[<p>p.s.  when you all post code snippets, you should use the &#8220;pre&#8221; and &#8220;code&#8221; format.  It makes the code easier to read.  Maybe Doug could edit the above posts and add the code and pre&#8217;s.</p>
<p>&lt;pre&gt;&lt;code&gt;</p>
<pre><code>
code goes here.
</code></pre>
<p>&lt;/pre&gt;&lt;/code&gt;</p>
<p>[edit by Doug to have both &lt;pre&gt; and &lt;code&gt; tags in suggestion.  Also edited old comment to do this.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
