{"id":275,"date":"2011-05-17T12:51:49","date_gmt":"2011-05-17T12:51:49","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2011\/05\/17\/what-a-difference-it-makes\/"},"modified":"2011-05-17T12:52:51","modified_gmt":"2011-05-17T12:52:51","slug":"what-a-difference-it-makes","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2011\/05\/17\/what-a-difference-it-makes\/","title":{"rendered":"What a Difference It Makes!"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>I've been exploring some older functions in MATLAB recently and trying to characterize them.  The function <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/diff.html\"><tt>diff<\/tt><\/a> is one of them.  And I realized it has a really unusual behavior when the optional <tt>dim<\/tt> input argument is omitted.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">From the Documentation<\/a><\/li>\r\n         <li><a href=\"#2\">!!!<\/a><\/li>\r\n         <li><a href=\"#6\">Is This Behavior Useful?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>From the Documentation<a name=\"1\"><\/a><\/h3>\r\n   <p>Look in the documentation for <tt>diff<\/tt> under the section entitled \"Tips\". You will see this.\r\n   <\/p>\r\n   <p><blockquote>Since each iteration of diff reduces the length of X\r\n      along dimension dim, it is possible to specify an order n sufficiently\r\n      high to reduce dim to a singleton (size(X,dim) = 1) dimension. When this\r\n      happens, diff continues calculating along the next nonsingleton\r\n      dimension.<\/blockquote>\r\n   <\/p>\r\n   <h3>!!!<a name=\"2\"><\/a><\/h3>\r\n   <p>What!  Try these statements.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">size(diff(ones(2,3,4),5))<\/pre><pre style=\"font-style:oblique\">ans =\r\n     1     1     2\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">diff(ones(2,3,4),6)<\/pre><pre style=\"font-style:oblique\">ans =\r\n     0\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">diff(ones(2,3,4),7)<\/pre><pre style=\"font-style:oblique\">ans =\r\n     []\r\n<\/pre><p>I know this behavior was not in the initial implementation of <tt>diff<\/tt> since I wrote it and would never be that cruel.  But it did get added later.  I can't see how it is useful at all. I will\r\n      always use a specific <tt>dim<\/tt> input myself to ensure robustness of my code.  But perhaps that's a failure of my imagination.\r\n   <\/p>\r\n   <h3>Is This Behavior Useful?<a name=\"6\"><\/a><\/h3>\r\n   <p>If anyone can come forth with a useful application of this aspect of the function <tt>diff<\/tt>, and prove it to me with working code in an application, I will spot the first such respondent with some MATLAB bling. Let\r\n      me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=275#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_ab22ce20506549abaa7916abec7efc74() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='ab22ce20506549abaa7916abec7efc74 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' ab22ce20506549abaa7916abec7efc74';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2011 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_ab22ce20506549abaa7916abec7efc74()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.12<br><\/p>\r\n<\/div>\r\n<!--\r\nab22ce20506549abaa7916abec7efc74 ##### SOURCE BEGIN #####\r\n%% What a Difference It Makes!\r\n% I've been exploring some older functions in MATLAB recently and trying to\r\n% characterize them.  The function\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/diff.html\r\n% |diff|> is one of them.  And I realized it has a really unusual behavior\r\n% when the optional |dim| input argument is omitted.  \r\n%% From the Documentation\r\n% Look in the documentation for |diff| under the section entitled \"Tips\".\r\n% You will see this.\r\n%\r\n% <html><blockquote>Since each iteration of diff reduces the length of X\r\n% along dimension dim, it is possible to specify an order n sufficiently\r\n% high to reduce dim to a singleton (size(X,dim) = 1) dimension. When this\r\n% happens, diff continues calculating along the next nonsingleton\r\n% dimension.<\/blockquote><\/html>\r\n%% !!!\r\n% What!  Try these statements.\r\nsize(diff(ones(2,3,4),5))\r\n%%\r\ndiff(ones(2,3,4),6)\r\n%%\r\ndiff(ones(2,3,4),7)\r\n%% \r\n% I know this behavior was not in the initial implementation of |diff|\r\n% since I wrote it and would never be that cruel.  But it did get added\r\n% later.  I can't see how it is useful at all. I will always use a specific\r\n% |dim| input myself to ensure robustness of my code.  But perhaps that's a\r\n% failure of my imagination.\r\n%% Is This Behavior Useful?\r\n% If anyone can come forth with a useful application of this aspect of the\r\n% function |diff|, and prove it to me with working code in an application,\r\n% I will spot the first such respondent with some MATLAB bling. Let me know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=275#respond here>.\r\n\r\n##### SOURCE END ##### ab22ce20506549abaa7916abec7efc74\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      I've been exploring some older functions in MATLAB recently and trying to characterize them.  The function diff is one of them.  And I realized it has a really unusual behavior when the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2011\/05\/17\/what-a-difference-it-makes\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/275"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=275"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/275\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}