{"id":98,"date":"2007-07-06T10:50:53","date_gmt":"2007-07-06T15:50:53","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2007\/07\/06\/make-code-n-d-safe\/"},"modified":"2007-07-06T13:27:35","modified_gmt":"2007-07-06T18:27:35","slug":"make-code-n-d-safe","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/07\/06\/make-code-n-d-safe\/","title":{"rendered":"Make Code N-D Safe"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>I've recently been asked some questions about programming safely to account for N-dimensional data.  Techniquest that are\r\n         tried-and-true for two dimensions don't all scale to N dimensions.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Code That's Not Recommended<\/a><\/li>\r\n         <li><a href=\"#4\">Code That is Safe for Higher Dimensions<\/a><\/li>\r\n         <li><a href=\"#5\">Benefits<\/a><\/li>\r\n         <li><a href=\"#6\">Your Advice?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Code That's Not Recommended<a name=\"1\"><\/a><\/h3>\r\n   <p>The following code doesn't catch dimensions higher than 2.<\/p><pre> if any(any(input))\r\n     output = doAlg(input);\r\n end<\/pre><p>This code is also not recommended because <tt>n<\/tt> will be a scalar even if <tt>input<\/tt> has more than 2 dimensions.  Depending on what is done in the loop, the output might be the wrong shape\/dimension (thanks\r\n      to Jeremy so I could clarify this).\r\n   <\/p><pre> [m,n] = size(input);\r\n for i=1:n\r\n     doSomething;\r\n end<\/pre><h3>Code That is Safe for Higher Dimensions<a name=\"4\"><\/a><\/h3>\r\n   <p>This code should work fine for any input.<\/p><pre> sz = size(input);\r\n if any(input(:))\r\n     output = doAlg(input);\r\n end\r\n output = reshape(output,sz);<\/pre><h3>Benefits<a name=\"5\"><\/a><\/h3>\r\n   <p>If you consider N-dimensional input when you write functions, you are less likely to find bugs or wrong answers when you go\r\n      to use it later.\r\n   <\/p><pre>   * The code is easier to understand without so many any statements.\r\n   * Code is robust to N-dimensional inputs.<\/pre><h3>Your Advice?<a name=\"6\"><\/a><\/h3>\r\n   <p>What techniques have you used to ensure your code works with all array shapes?  List them <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=98\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_e96c09344ca14a5493256a237a8e3fbd() {\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='e96c09344ca14a5493256a237a8e3fbd ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' e96c09344ca14a5493256a237a8e3fbd';\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 2007 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_e96c09344ca14a5493256a237a8e3fbd()\"><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.4<br><\/p>\r\n<\/div>\r\n<!--\r\ne96c09344ca14a5493256a237a8e3fbd ##### SOURCE BEGIN #####\r\n%% Make Code N-D Safe\r\n% I've recently been asked some questions about programming safely to\r\n% account for N-dimensional data.  Techniquest that are tried-and-true\r\n% for two dimensions don't all scale to N dimensions.\r\n%% Code That's Not Recommended\r\n% The following code doesn't catch dimensions higher than 2.\r\n%\r\n%%\r\n%\r\n%   if any(any(input))\r\n%       output = doAlg(input);\r\n%   end   \r\n%\r\n%% \r\n% This code is also not recommended because |n| will be a scalar even if\r\n% |input|\r\n% has more than 2 dimensions.  Depending on what is done in the loop, the\r\n% output might be the wrong shape\/dimension (thanks to Jeremy so I could\r\n% clarify this).\r\n% \r\n%   [m,n] = size(input);\r\n%   for i=1:n\r\n%       doSomething;\r\n%   end \r\n\r\n\r\n%% Code That is Safe for Higher Dimensions\r\n% This code should work fine for any input.\r\n% \r\n%   sz = size(input);\r\n%   if any(input(:))\r\n%       output = doAlg(input);\r\n%   end\r\n%   output = reshape(output,sz);\r\n% \r\n\r\n%% Benefits\r\n% If you consider N-dimensional input when you write functions, you are\r\n% less likely to find bugs or wrong answers when you go to use it later.\r\n% \r\n%     * The code is easier to understand without so many any statements.\r\n%     * Code is robust to N-dimensional inputs.\r\n%% Your Advice?\r\n% What techniques have you used to ensure your code works with all array\r\n% shapes?  List them <https:\/\/blogs.mathworks.com\/loren\/?p=98 here>.\r\n##### SOURCE END ##### e96c09344ca14a5493256a237a8e3fbd\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      I've recently been asked some questions about programming safely to account for N-dimensional data.  Techniquest that are\r\n         tried-and-true for two dimensions don't all scale to N... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/07\/06\/make-code-n-d-safe\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,14],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/98"}],"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=98"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/98\/revisions"}],"predecessor-version":[{"id":3122,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/98\/revisions\/3122"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=98"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=98"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=98"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}