{"id":215,"date":"2010-01-27T14:59:33","date_gmt":"2010-01-27T14:59:33","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2010\/01\/27\/matlab-behavior-for\/"},"modified":"2010-01-19T15:03:12","modified_gmt":"2010-01-19T15:03:12","slug":"matlab-behavior-for","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2010\/01\/27\/matlab-behavior-for\/","title":{"rendered":"MATLAB Behavior: for"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Conventional wisdom for programming MATLAB used to be that using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/for.html\"><tt>for<\/tt><\/a> loops automatically forced a program to suffer from poor performance. Since MATLAB R13 (version 6.5), MATLAB has taken advantage\r\n         of some innovations that accelerate many <tt>for<\/tt> loops so the code has performance on par with either vectorized code or code written in a lower level language such as C\r\n         or Fortran.  Obviously, details matter here.  One thing that most people, even at MathWorks (!) don't appreciate is that the\r\n         <tt>for<\/tt> loop has richer behavior than simply looping over single elements at a time.  An informal hallway survey near my office found\r\n         that even among experienced MATLAB programmers, far fewer than 50% knew about this behavior.  Time to come clean.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Exploring the Behavior of 'for'<\/a><\/li>\r\n         <li><a href=\"#3\">Behavior of for<\/a><\/li>\r\n         <li><a href=\"#5\">More Examples<\/a><\/li>\r\n         <li><a href=\"#7\">What about Higher Dimensions?<\/a><\/li>\r\n         <li><a href=\"#11\">Possible Uses<\/a><\/li>\r\n         <li><a href=\"#12\">What Do You Use?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Exploring the Behavior of 'for'<a name=\"1\"><\/a><\/h3>\r\n   <p>The MATLAB <tt>for<\/tt> statement is both more powerful and more subtle than many people realize because of the way it lets you iterate directly\r\n      over an array rather than making use of explicit indices or subscripts. Here's an example that displays the logarithms of\r\n      the positive numbers in a row vector:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x = [1 pi -17 1.3 289 -exp(1) -42];\r\n\r\n<span style=\"color: #0000FF\">for<\/span> pnum = x(x &gt; 0)\r\n    disp(<span style=\"color: #A020F0\">'Iterating'<\/span>)\r\n    log(pnum)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">Iterating\r\nans =\r\n     0\r\nIterating\r\nans =\r\n           1.1447298858494\r\nIterating\r\nans =\r\n         0.262364264467491\r\nIterating\r\nans =\r\n          5.66642668811243\r\n<\/pre><p>This code is streamlined in comparison to the following, which uses an explicit index <tt>ind<\/tt>, with equivalent results (except for the details on the output that I added here).\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">pnums = x(x &gt; 0);\r\n<span style=\"color: #0000FF\">for<\/span> ind = 1:numel(pnums)\r\n    disp([<span style=\"color: #A020F0\">'Iterating...   Value #'<\/span>,int2str(ind)])\r\n    log(pnums(ind))\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">Iterating...   Value #1\r\nans =\r\n     0\r\nIterating...   Value #2\r\nans =\r\n           1.1447298858494\r\nIterating...   Value #3\r\nans =\r\n         0.262364264467491\r\nIterating...   Value #4\r\nans =\r\n          5.66642668811243\r\n<\/pre><h3>Behavior of for<a name=\"3\"><\/a><\/h3>\r\n   <p>To use <tt>for<\/tt> as in the first example, you need to understand that <tt>for<\/tt> loops do not iterate over the first dimension of an array, but only over dimensions 2 to the maximum array dimension.  See\r\n      this by transposing the input vector (turning it into a column).\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">xt = x.'\r\n<span style=\"color: #0000FF\">for<\/span> pnum = xt(xt &gt; 0)\r\n    disp(<span style=\"color: #A020F0\">'Iterating'<\/span>)\r\n    log(pnum)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">xt =\r\n                         1\r\n          3.14159265358979\r\n                       -17\r\n                       1.3\r\n                       289\r\n         -2.71828182845905\r\n                       -42\r\nIterating\r\nans =\r\n                         0\r\n           1.1447298858494\r\n         0.262364264467491\r\n          5.66642668811243\r\n<\/pre><p>We see one iteration, with <tt>pnum<\/tt> taking on the <tt>4-by-1<\/tt> value <tt>[1 pi 1.3 289]'<\/tt>. In contrast, the version with the explicit index works the same way whatever the shape of x.\r\n   <\/p>\r\n   <h3>More Examples<a name=\"5\"><\/a><\/h3>\r\n   <p>This iterates over s.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">for<\/span> s = [1,-2,8,pi,17], disp(<span style=\"color: #A020F0\">'Iterating'<\/span>), disp(s), <span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">Iterating\r\n     1\r\nIterating\r\n    -2\r\nIterating\r\n     8\r\nIterating\r\n          3.14159265358979\r\nIterating\r\n    17\r\n<\/pre><p>This doesn't iterate, but processes the entire column as one entity.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">for<\/span> s = [1,-2,8,pi,17]', disp(<span style=\"color: #A020F0\">'Iterating'<\/span>), disp(s), <span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">Iterating\r\n                         1\r\n                        -2\r\n                         8\r\n          3.14159265358979\r\n                        17\r\n<\/pre><h3>What about Higher Dimensions?<a name=\"7\"><\/a><\/h3>\r\n   <p>This iterates over the 2nd and 3rd dimensions of A.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">A = (1:12);\r\nA = reshape(A,[2 2 3])<\/pre><pre style=\"font-style:oblique\">A(:,:,1) =\r\n     1     3\r\n     2     4\r\nA(:,:,2) =\r\n     5     7\r\n     6     8\r\nA(:,:,3) =\r\n     9    11\r\n    10    12\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">for<\/span> k = A, disp(<span style=\"color: #A020F0\">'iterating'<\/span>), disp(k), <span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">iterating\r\n     1\r\n     2\r\niterating\r\n     3\r\n     4\r\niterating\r\n     5\r\n     6\r\niterating\r\n     7\r\n     8\r\niterating\r\n     9\r\n    10\r\niterating\r\n    11\r\n    12\r\n<\/pre><p>The documentation makes it clear that <tt>for<\/tt> does not iterate over the first dimension. <tt>for<\/tt> <i>does<\/i> iterate over all of the dimensions of A except for the first (row) dimension. You can predict the number of iterations evaluating\r\n      this.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">numel(A,1,<span style=\"color: #A020F0\">':'<\/span>)<\/pre><pre style=\"font-style:oblique\">ans =\r\n     6\r\n<\/pre><p>Returning to the first example, <tt>numel(x(x &gt;= 0),1,':')<\/tt> evaluates to 4 when <tt>x<\/tt> is a row vector and <tt>1<\/tt> when x is a column vector.\r\n   <\/p>\r\n   <h3>Possible Uses<a name=\"11\"><\/a><\/h3>\r\n   <p>Why use this version of <tt>for<\/tt>?  Suppose you have a large dataset and the vectorized calculations can't take full advantage of functions such as <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/bsxfun.html\"><tt>bsxfun<\/tt><\/a>. You may have a case where the memory tradeoff for vectorization is too high.  But you don't want to make function calls\r\n      for each array element since the function call overhead can get high as well.  A possibly good compromise is to essentially\r\n      process the data in chunks, perhaps by 'virtual' columns.  That way the function call overhead is more limited as is the memory.\r\n       To get the best outcome for this approach, you should preallocate the output array and assign into it with proper indexing\r\n      as you loop.\r\n   <\/p>\r\n   <h3>What Do You Use?<a name=\"12\"><\/a><\/h3>\r\n   <p>Did you know about this <tt>for<\/tt> behavior?  Do you ever take advantage of it?  What strategies do you use for trading off memory use and function call overhead?\r\n       Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=215#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_5fc282c448fd40e4b8492432a01f1968() {\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='5fc282c448fd40e4b8492432a01f1968 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 5fc282c448fd40e4b8492432a01f1968';\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 2010 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_5fc282c448fd40e4b8492432a01f1968()\"><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.9<br><\/p>\r\n<\/div>\r\n<!--\r\n5fc282c448fd40e4b8492432a01f1968 ##### SOURCE BEGIN #####\r\n%% MATLAB Behavior: for\r\n% Conventional wisdom for programming MATLAB used to be that using\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/for.html |for|>\r\n% loops automatically forced a program to suffer from poor performance.\r\n% Since MATLAB R13 (version 6.5), MATLAB has taken advantage of some\r\n% innovations that accelerate many |for| loops so the code has performance\r\n% on par with either vectorized code or code written in a lower level\r\n% language such as C or Fortran.  Obviously, details matter here.  One\r\n% thing that most people, even at MathWorks (!) don't appreciate is that\r\n% the |for| loop has richer behavior than simply looping over single\r\n% elements at a time.  An informal hallway survey near my office found that\r\n% even among experienced MATLAB programmers, far fewer than 50% knew about\r\n% this behavior.  Time to come clean.\r\n%% Exploring the Behavior of 'for'\r\n% The MATLAB |for| statement is both more powerful and more subtle than\r\n% many people realize because of the way it lets you iterate directly over\r\n% an array rather than making use of explicit indices or subscripts. Here's\r\n% an example that displays the logarithms of the positive numbers in\r\n% a row vector:\r\n\r\nx = [1 pi -17 1.3 289 -exp(1) -42];\r\n\r\nfor pnum = x(x > 0)\r\n    disp('Iterating')\r\n    log(pnum)\r\nend\r\n%%\r\n% This code is streamlined in comparison to the following, which \r\n% uses an explicit index |ind|, with equivalent results (except for the\r\n% details on the output that I added here).\r\n\r\npnums = x(x > 0);\r\nfor ind = 1:numel(pnums)\r\n    disp(['Iterating...   Value #',int2str(ind)])\r\n    log(pnums(ind))\r\nend\r\n\r\n\r\n%% Behavior of |for|\r\n% To use |for| as in the first example, you need to understand that |for|\r\n% loops do not iterate over the first dimension of an array, but only over\r\n% dimensions 2 to the maximum array dimension.  See this by transposing the\r\n% input vector (turning it into a column).\r\nxt = x.'\r\nfor pnum = xt(xt > 0)\r\n    disp('Iterating')\r\n    log(pnum)\r\nend\r\n%%\r\n% We see one iteration, with |pnum| taking on the |4-by-1| value\r\n% |[1 pi 1.3 289]'|. In contrast, the version with the explicit index works\r\n% the same way whatever the shape of x.\r\n\r\n%% More Examples\r\n% This iterates over s.\r\nfor s = [1,-2,8,pi,17], disp('Iterating'), disp(s), end\r\n\r\n%%\r\n% This doesn't iterate, but processes the entire column as one entity.\r\nfor s = [1,-2,8,pi,17]', disp('Iterating'), disp(s), end\r\n\r\n%% What about Higher Dimensions?\r\n% This iterates over the 2nd and 3rd dimensions of A.\r\nA = (1:12);\r\nA = reshape(A,[2 2 3])\r\n%%\r\nfor k = A, disp('iterating'), disp(k), end\r\n\r\n%%\r\n% The documentation makes it clear that |for| does not iterate over the\r\n% first dimension. |for| _does_ iterate over all of the dimensions of A\r\n% except for the first (row) dimension. You can predict the number of\r\n% iterations evaluating this.\r\nnumel(A,1,':')\r\n\r\n%%\r\n% Returning to the first example, |numel(x(x >= 0),1,':')| evaluates to 4 when\r\n% |x| is a row vector and |1| when x is a column vector.\r\n%% Possible Uses\r\n% Why use this version of |for|?  Suppose you have a large dataset and the\r\n% vectorized calculations can't take full advantage of functions such as \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/bsxfun.html |bsxfun|>.\r\n% You may have a case where the memory tradeoff for vectorization is too\r\n% high.  But you don't want to make function calls for each array element\r\n% since the function call overhead can get high as well.  A possibly good\r\n% compromise is to essentially process the data in chunks, perhaps by\r\n% 'virtual' columns.  That way the function call overhead is more limited\r\n% as is the memory.  To get the best outcome for this approach, you should\r\n% preallocate the output array and assign into it with proper indexing as\r\n% you loop.\r\n%% What Do You Use?\r\n% Did you know about this |for| behavior?  Do you ever take advantage of\r\n% it?  What strategies do you use for trading off memory use and function\r\n% call overhead?  Let me know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=215#respond here>.\r\n\r\n##### SOURCE END ##### 5fc282c448fd40e4b8492432a01f1968\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Conventional wisdom for programming MATLAB used to be that using for loops automatically forced a program to suffer from poor performance. Since MATLAB R13 (version 6.5), MATLAB has... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2010\/01\/27\/matlab-behavior-for\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,12],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/215"}],"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=215"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/215\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}