{"id":335,"date":"2010-06-29T12:47:14","date_gmt":"2010-06-29T16:47:14","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2010\/06\/29\/dim-terminology\/"},"modified":"2019-10-29T13:30:31","modified_gmt":"2019-10-29T17:30:31","slug":"dim-terminology","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2010\/06\/29\/dim-terminology\/","title":{"rendered":"&#8220;DIM&#8221; terminology"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>I've been bothered recently by a MATLAB terminology question: How should we describe the <tt>dim<\/tt> argument in functions such as <tt>sum<\/tt>? The documentation for <tt>sum<\/tt> says:\r\n   <\/p><pre>  B = sum(A,dim) sums along the dimension of A specified by\r\n  scalar dim. The dim input is an integer value from 1 to N,\r\n  where N is the number of dimensions in A. Set dim to 1 to\r\n  compute the sum of each column, 2 to sum rows, etc.<\/pre><p>For example:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">a = [1 2 3; 4 5 6; 7 8 9]<\/pre><pre style=\"font-style:oblique\">\r\na =\r\n\r\n     1     2     3\r\n     4     5     6\r\n     7     8     9\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">sum(a, 1)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n    12    15    18\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">sum(a, 2)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n     6\r\n    15\r\n    24\r\n\r\n<\/pre><p>For some people this terminology seems to be quite confusing.  I started thinking about this when I saw a user question about it recently.  In that question, the mental model leading to confusion seems to be this: dimension 1 is the row\r\n      dimension, so <tt>sum(a,1)<\/tt> should sum each row, but MATLAB sums each column.\r\n   <\/p>\r\n   <p>This a hard mental model to shake, apparently, and it makes me wonder if the phrase \"along the dimension\" isn't clear enough.<\/p>\r\n   <p>When you look at equations instead of ambiguous prose, it's easier to see the logic of the <tt>dim<\/tt> argument. Consider, for example, an M-by-N matrix <tt>A<\/tt>. <tt>sum(A,1)<\/tt> is doing this:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/sum_dim_terminology_eq92148.png\"> <\/p>\r\n   <p>The variable of summation, <i>i<\/i>, is the <i>first<\/i> subscript of <i>A<\/i>.\r\n   <\/p>\r\n   <p><tt>sum(A,2)<\/tt> is doing this:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/sum_dim_terminology_eq51768.png\"> <\/p>\r\n   <p>The variable of summation, <i>j<\/i>, is the <i>second<\/i> subscript of <i>A<\/i>.\r\n   <\/p>\r\n   <p>This pattern extends nicely to higher dimensions.<\/p>\r\n   <p>So, dear readers, can you suggest a better way to document the meaning of the <tt>dim<\/tt> argument? Or should we just leave it be?\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_721c8c0b59d2431fb562426f65f9542d() {\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='721c8c0b59d2431fb562426f65f9542d ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 721c8c0b59d2431fb562426f65f9542d';\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 = 'Steve Eddins';\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_721c8c0b59d2431fb562426f65f9542d()\"><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.10<br><\/p>\r\n<\/div>\r\n<!--\r\n721c8c0b59d2431fb562426f65f9542d ##### SOURCE BEGIN #####\r\n%%\r\n% I've been bothered recently by a MATLAB terminology question: How should we\r\n% describe the |dim| argument in functions such as |sum|? The documentation for\r\n% |sum| says:\r\n%\r\n%    B = sum(A,dim) sums along the dimension of A specified by \r\n%    scalar dim. The dim input is an integer value from 1 to N, \r\n%    where N is the number of dimensions in A. Set dim to 1 to \r\n%    compute the sum of each column, 2 to sum rows, etc. \r\n%\r\n% For example:\r\n\r\na = [1 2 3; 4 5 6; 7 8 9]\r\n\r\n%%\r\nsum(a, 1)\r\n\r\n%%\r\nsum(a, 2)\r\n\r\n%%\r\n% For some people this terminology\r\n% seems to be quite confusing.  I started thinking about this when I saw a\r\n% <http:\/\/stackoverflow.com\/questions\/2651267\/why-is-sumx-1-the-sum-of-the-columns-in-matlab \r\n% question about it> posted on stackoverflow.  In that question, the mental\r\n% model leading to confusion seems to be this: dimension 1 is the row dimension,\r\n% so |sum(a,1)| should sum each row, but MATLAB sums each column.\r\n%\r\n% This a hard mental model to shake, apparently, and it makes me wonder if the\r\n% phrase \"along the dimension\" isn't clear enough.\r\n%\r\n% When you look at equations instead of ambiguous prose, it's easier to see the\r\n% logic of the |dim| argument. Consider, for example, an M-by-N matrix |A|.\r\n% |sum(A,1)| is doing this:\r\n%\r\n% $$ s(j) = \\sum_{i=1}^{M} A(i, j),\\;\\; 1 \\leq j \\leq N $$\r\n%\r\n% The variable of summation, _i_, is the _first_ subscript of _A_.\r\n%\r\n% |sum(A,2)| is doing this:\r\n%\r\n% $$ s(i) = \\sum_{j=1}^{N} A(i, j),\\;\\; 1 \\leq i \\leq M $$\r\n%\r\n% The variable of summation, _j_, is the _second_ subscript of _A_.\r\n%\r\n% This pattern extends nicely to higher dimensions.\r\n%\r\n% So, dear readers, can you suggest a better way to document the meaning of the\r\n% |dim| argument? Or should we just leave it be?\r\n\r\n##### SOURCE END ##### 721c8c0b59d2431fb562426f65f9542d\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   I've been bothered recently by a MATLAB terminology question: How should we describe the dim argument in functions such as sum? The documentation for sum says:\r\n     B = sum(A,dim) sums along... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2010\/06\/29\/dim-terminology\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[126],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/335"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=335"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/335\/revisions"}],"predecessor-version":[{"id":3687,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/335\/revisions\/3687"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}