<?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: MATLAB Puzzler: Finding the two closest points</title>
	<atom:link href="http://blogs.mathworks.com/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/</link>
	<description>&#60;a href=&#34;http://www.mathworks.com/matlabcentral/fileexchange/loadAuthor.do?objectId=1093599&#38;objectType=author&#34;&#62;Brett&#60;/a&#62; &#38; &#60;a href=&#34;http://www.mathworks.com/matlabcentral/fileexchange/loadAuthor.do?objectId=1094142&#38;objectType=author&#34;&#62;Jiro&#60;/a&#62; share favorite user-contributed submissions from the File Exchange.</description>
	<lastBuildDate>Fri, 10 Feb 2012 16:12:39 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Doug</title>
		<link>http://blogs.mathworks.com/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-13416</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Mon, 01 Feb 2010 19:00:29 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-13416</guid>
		<description>@Rupa,

Could you do this algorithm and then remove the two points and run again?

Not efficient, but could be coded from this existing code easy.  If you are not to worried about speed, or are doing not many of these, it would be enough.

Doug</description>
		<content:encoded><![CDATA[<p>@Rupa,</p>
<p>Could you do this algorithm and then remove the two points and run again?</p>
<p>Not efficient, but could be coded from this existing code easy.  If you are not to worried about speed, or are doing not many of these, it would be enough.</p>
<p>Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rupa</title>
		<link>http://blogs.mathworks.com/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-13411</link>
		<dc:creator>Rupa</dc:creator>
		<pubDate>Thu, 28 Jan 2010 16:25:03 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-13411</guid>
		<description>Hi, sorry to digress but I have a similar problem, I think. I would like to find the three (or n) smallest distances but each point can be used only once. I.e. I have two arrays A &amp; B and would like to find n unique combinations (i.e. the sum of an element in array A and an element in array B) that give me the three smallest sums. But each array element can only appear in one combination. Thanks so much for your help.</description>
		<content:encoded><![CDATA[<p>Hi, sorry to digress but I have a similar problem, I think. I would like to find the three (or n) smallest distances but each point can be used only once. I.e. I have two arrays A &amp; B and would like to find n unique combinations (i.e. the sum of an element in array A and an element in array B) that give me the three smallest sums. But each array element can only appear in one combination. Thanks so much for your help.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://blogs.mathworks.com/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-13138</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Thu, 23 Apr 2009 13:32:30 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-13138</guid>
		<description>@lakshmi

http://blogs.mathworks.com/videos/category/gui-or-guide/

You should find something you like here.

Doug</description>
		<content:encoded><![CDATA[<p>@lakshmi</p>
<p><a href="http://blogs.mathworks.com/videos/category/gui-or-guide/" rel="nofollow">http://blogs.mathworks.com/videos/category/gui-or-guide/</a></p>
<p>You should find something you like here.</p>
<p>Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: lakshmi narayanan</title>
		<link>http://blogs.mathworks.com/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-13135</link>
		<dc:creator>lakshmi narayanan</dc:creator>
		<pubDate>Thu, 23 Apr 2009 01:38:13 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-13135</guid>
		<description>this was awesome. can you give me programs on handling a item selection from pop up menu</description>
		<content:encoded><![CDATA[<p>this was awesome. can you give me programs on handling a item selection from pop up menu</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yi Cao</title>
		<link>http://blogs.mathworks.com/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-11511</link>
		<dc:creator>Yi Cao</dc:creator>
		<pubDate>Fri, 06 Jun 2008 19:49:10 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-11511</guid>
		<description>Doug,

Personally, I like the node count because it encourages to use meaningful variable names. A variable with a name &#039;d&#039; has the same node count as using name &#039;distance&#039;, 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/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-11505</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Fri, 06 Jun 2008 12:57:16 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-11505</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 &quot;thumbs up/down&quot; 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/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-11502</link>
		<dc:creator>Yi Cao</dc:creator>
		<pubDate>Fri, 06 Jun 2008 08:27:00 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-11502</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]&#039;);
[i,j] = find(squareform(d)==min(d),1,&#039;first&#039;);&lt;/code&gt;
&lt;/pre&gt;

The length of code can be checked as follows:

&lt;pre&gt;
&lt;code&gt;t = mtree(&#039;[i,j]=find(squareform(pdist([x; y]&#039;&#039;))==min(pdist([x; y]&#039;&#039;)),1,&#039;&#039;first&#039;&#039;)&#039;)&lt;/code&gt;
&lt;/pre&gt;
which gives 33 nodes.

&lt;pre&gt;
&lt;code&gt;t = mtree(&#039;d=pdist([x; y]&#039;&#039;);[i,j]=find(squareform(d)==min(d),1,&#039;&#039;first&#039;&#039;)&#039;)&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/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-11501</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Fri, 06 Jun 2008 00:55:11 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-11501</guid>
		<description>
&lt;pre&gt;&lt;code&gt;[i,j]=find(squareform(pdist([x; y]&#039;))==min(pdist([x; y]&#039;)),1,&#039;first&#039;)&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/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-11498</link>
		<dc:creator>Yi Cao</dc:creator>
		<pubDate>Thu, 05 Jun 2008 23:07:32 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-11498</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]&#039;);
[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.&#039;)));
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/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-11494</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Thu, 05 Jun 2008 14:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/06/02/matlab-puzzler-finding-the-two-closest-points/#comment-11494</guid>
		<description>@Dan: You might want to increase the &#039;dummy value&#039; 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>
</channel>
</rss>

