{"id":9,"date":"2008-09-25T11:12:06","date_gmt":"2008-09-25T15:12:06","guid":{"rendered":"https:\/\/blogs.mathworks.com\/videos\/2008\/09\/25\/puzzler-cleverness-needed\/"},"modified":"2008-09-25T11:12:06","modified_gmt":"2008-09-25T15:12:06","slug":"puzzler-cleverness-needed","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/videos\/2008\/09\/25\/puzzler-cleverness-needed\/","title":{"rendered":"Puzzler: Cleverness needed"},"content":{"rendered":"<p>I have been working on a side project and I found I needed the following algorithm:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/videos\/..\/images\/videos\/2008\/09\/target.jpg\" alt=\"MATLAB puzzler\" \/><\/p>\n<p>Given a binary five by five matrix, I need to find the &#8216;1&#8217; value that is in the position that has the lowest value in the spiral map above.\u00a0 I call that the target square. Once that target is found, then I need to return a <em>delta x<\/em> and <em>delta y<\/em> that will get me closer to that\u00a0 target.\u00a0 <em>Delta x<\/em> and <em>delta y<\/em> must be taken relative to the center square, and can only be from the set [-1, 0, 1].<\/p>\n<p>As an example, given the matrix:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blogs.mathworks.com\/images\/videos\/2008\/09\/targetbinary.jpg\" alt=\"MATLAB puzzler\" width=\"494\" height=\"375\" \/><\/p>\n<p>The &#8216;1&#8217; in the (2,2) position in the matrix is the target because it maps to 9, and the other two &#8216;1&#8217; values map to larger values, 11 and 18.\u00a0 From here, the <em>delta x<\/em> is -1 and <em>delta y<\/em> is 1.<\/p>\n<p>If the target was 18, then the values would have been <em>delta x<\/em> of 0 and <em>delta y<\/em> of -1.<\/p>\n<p>A final edge case, if the input matrix were all zeros, then <em>delta x<\/em> and <em>delta y<\/em> should be chosen randomly, but still pulled from the set [-1, 0, 1].<\/p>\n<hr \/>\n<p>This is not a particularly difficult algorithm to program if you use 25 <strong>if, elseif<\/strong> statements.  However, I am looking to be a bit more clever with this.\u00a0 Please post your solution in the comments, make it a function of this form:<\/p>\n<p>\n<strong>[deltaX, deltaY] = puzzler(inMatrix)<\/strong><\/p>\n<hr \/>\n<p>As always with these puzzlers, be sure to use the proper tags in the comments.<\/p>\n<pre><code>\r\n&lt;pre> &lt;code>\r\n<strong>[deltaX, deltaY] = puzzler(inMatrix)<\/strong>\r\n%% All the code so someone can just copy and paste it from the comments.\r\n&lt;\/code> &lt;\/pre>\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have been working on a side project and I found I needed the following algorithm:<\/p>\n<p>Given a binary five by five matrix, I need to find the &#8216;1&#8217; value that is in the position that has the&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/videos\/2008\/09\/25\/puzzler-cleverness-needed\/\">read more >><\/a><\/p>\n","protected":false},"author":68,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/9"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/users\/68"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/comments?post=9"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/9\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/media?parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/categories?post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/tags?post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}