{"id":204,"date":"2009-11-04T19:30:53","date_gmt":"2009-11-04T19:30:53","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2009\/11\/04\/calculus-with-empty-arrays\/"},"modified":"2018-01-08T15:23:59","modified_gmt":"2018-01-08T20:23:59","slug":"calculus-with-empty-arrays","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2009\/11\/04\/calculus-with-empty-arrays\/","title":{"rendered":"Calculus with Empty Arrays"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>MATLAB has had empty arrays since before I started using the program. When I started, the only size empty array was <tt>0x0<\/tt>.  When version 5 was released, empty arrays came along for the N-dimensional ride and got more shapely.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#2\">From the Newsgroup<\/a><\/li>\r\n         <li><a href=\"#4\">Dimensions Matter in MATLAB<\/a><\/li>\r\n         <li><a href=\"#11\">Empty Array Shapes<\/a><\/li>\r\n         <li><a href=\"#12\">Reference<\/a><\/li>\r\n         <li><a href=\"#13\">Got an Empty Question?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Even relatively simple expressions involving empty arrays cause confusion from time to time, especially in concert with other\r\n      rules in MATLAB (such as <tt>NaN<\/tt> values usually propagate from inputs to outputs).  Let's play around a little with some empty arrays to get some insight.\r\n   <\/p>\r\n   <h3>From the Newsgroup<a name=\"2\"><\/a><\/h3>\r\n   <p>This <a>post<\/a> on the MATLAB newsgroup motivated me to talk about empty arrays.  Here's is an excerpt from the original post:\r\n   <\/p><pre> I knew that Nan+4 = NaN, but why is []+4 = [] ? Is there more 'black hole\r\n behaviour' I should know about?<\/pre><h3>Dimensions Matter in MATLAB<a name=\"4\"><\/a><\/h3>\r\n   <p>Dimensions matter in MATLAB and you will get error messages when dimensions don't agree.  Early on, we found it was convenient\r\n      to treat scalars as an exception with operators (e.g., <tt><b>,.<\/b><\/tt>) and to treat them as if they were expanded to the size of the other operand.  So\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">rand(3,3) + pi<\/pre><pre style=\"font-style:oblique\">ans =\r\n          4.10648118878907          4.09875960183274          3.28347899221701\r\n          3.29920573526734          3.62696830231263          3.56335393621607\r\n          4.11218543535041          3.94187312247859          4.05732817877886\r\n<\/pre><p>adds the value <tt>pi<\/tt> to the random <tt>3x3<\/tt> just created.  It's as if a <tt>3x3<\/tt> constant array filled with the value <tt>pi<\/tt> was created and added elementwise to the other array.\r\n   <\/p>\r\n   <p>So what does that mean for an empty operand?  Its size has at least one 0 value.  So MATLAB expands <tt>pi<\/tt> in this expression <tt>[] + pi<\/tt> to be the same size as <tt>[]<\/tt> (which happens to be <tt>0x0<\/tt> here).  When that happens, MATLAB creates a second empty array, and then adds the two empty arrays. Hence we get\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[] + pi<\/pre><pre style=\"font-style:oblique\">ans =\r\n     []\r\n<\/pre><p>returing an empty output.<\/p>\r\n   <p>MATLAB applies the scalar expansion rule to operators.  So, for example, <i>size(anything+scalar)<\/i> is <i>size(anything)<\/i>.  As a logical but sometimes surprising consequence, empty arrays often (but not always) propagate through calculations.\r\n       When doesn't that happen?  With some functions where mathematically logical analysis demands different results.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">sum([])<\/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)\">prod([])<\/pre><pre style=\"font-style:oblique\">ans =\r\n     1\r\n<\/pre><p>So, why is the sum zero and the product 1?  Because they are the identity elements (as in group theory) for sum and product\r\n      respectively.  Think about how to start the computation for a sum or a product and how you would initialize the first value.\r\n       That's the value given before adding or multiplying any of the array elements, hence the values 0 and 1 respectively.\r\n   <\/p>\r\n   <h3>Empty Array Shapes<a name=\"11\"><\/a><\/h3>\r\n   <p>Empty arrays can be N-dimensional, and don't need all dimensions to be 0. However, they still must obey the rules about dimensions\r\n      needing to agree.  There are consequences that may surprise you, but they do follow logically.  Here's an example of trying\r\n      to add two empty arrays, ending up in an error!\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">a = zeros(0,1)\r\nb = zeros(1,0)\r\n<span style=\"color: #0000FF\">try<\/span>\r\n    a+b\r\n<span style=\"color: #0000FF\">catch<\/span> MEplus\r\n    fprintf(<span style=\"color: #A020F0\">'\\n'<\/span>)\r\n    disp(MEplus.message)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">a =\r\n   Empty matrix: 0-by-1\r\nb =\r\n   Empty matrix: 1-by-0\r\n\r\nMatrix dimensions must agree.\r\n<\/pre><h3>Reference<a name=\"12\"><\/a><\/h3>\r\n   <p>For more information about empty arrays, check out <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/math\/f1-86359.html#f1-86384\">this page<\/a> in the documentation.\r\n   <\/p>\r\n   <h3>Got an Empty Question?<a name=\"13\"><\/a><\/h3>\r\n   <p>Got any empty questions (really, questions about empty!)?  Or comments? Post them <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=204#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_99a0320200324b5cac73992d594d08c9() {\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='99a0320200324b5cac73992d594d08c9 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 99a0320200324b5cac73992d594d08c9';\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 2009 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_99a0320200324b5cac73992d594d08c9()\"><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\n99a0320200324b5cac73992d594d08c9 ##### SOURCE BEGIN #####\r\n%% Calculus with Empty Arrays\r\n% MATLAB has had empty arrays since before I started using the program.\r\n% When I started, the only size empty array was |0x0|.  When\r\n% version 5 was released, empty arrays came along for the N-dimensional\r\n% ride and got more shapely.\r\n%%\r\n% Even relatively simple expressions involving\r\n% empty arrays cause confusion from time to time, especially in concert\r\n% with other rules in MATLAB (such as |NaN| values usually propagate from\r\n% inputs to outputs).  Let's play around a little with some empty arrays to\r\n% get some insight.\r\n\r\n%% From the Newsgroup\r\n% This <http:\/\/view_thread\/263290#687525 post>\r\n% on the MATLAB newsgroup motivated me to talk about empty arrays.  Here's\r\n% is an excerpt from the original post:\r\n%%\r\n% \r\n%   I knew that Nan+4 = NaN, but why is []+4 = [] ? Is there more 'black hole \r\n%   behaviour' I should know about?\r\n%% Dimensions Matter in MATLAB\r\n% Dimensions matter in MATLAB and you will get error messages when\r\n% dimensions don't agree.  Early on, we found it was convenient to treat\r\n% scalars as an exception with operators (e.g., |*,.*|) and to treat them\r\n% as if they were expanded to the size of the other operand.  So\r\nrand(3,3) + pi\r\n%%\r\n% adds the value |pi| to the random |3x3| just created.  It's as if a |3x3|\r\n% constant array filled with the value |pi| was created and added\r\n% elementwise to the other array.\r\n%%\r\n% So what does that mean for an empty operand?  Its size has at least one\r\n% 0 value.  So MATLAB expands |pi| in this expression |[] + pi| to be the\r\n% same size as |[]| (which happens to be |0x0| here).  When that happens,\r\n% MATLAB creates a second empty array, and then adds the two empty arrays.\r\n% Hence we get\r\n[] + pi\r\n%%\r\n% returing an empty output.\r\n%%\r\n% MATLAB applies the scalar expansion rule to operators.  So, for example,\r\n% _size(anything+scalar)_ is _size(anything)_.  As a logical but sometimes\r\n% surprising consequence, empty arrays often (but not always) propagate\r\n% through calculations.  When doesn't that happen?  With some functions\r\n% where mathematically logical analysis demands different results.\r\nsum([])\r\n%%\r\nprod([])\r\n%%\r\n% So, why is the sum zero and the product 1?  Because they are the identity\r\n% elements (as in group theory) for sum and product respectively.  Think\r\n% about how to start the computation for a sum or a product and how you\r\n% would initialize the first value.  That's the value given before adding\r\n% or multiplying any of the array elements, hence the values 0 and 1\r\n% respectively.\r\n%% Empty Array Shapes\r\n% Empty arrays can be N-dimensional, and don't need all dimensions to be 0.\r\n% However, they still must obey the rules about dimensions needing to\r\n% agree.  There are consequences that may surprise you, but they do follow\r\n% logically.  Here's an example of trying to add two empty arrays, ending\r\n% up in an error!\r\na = zeros(0,1)\r\nb = zeros(1,0)\r\ntry\r\n    a+b\r\ncatch MEplus\r\n    fprintf('\\n')\r\n    disp(MEplus.message)\r\nend\r\n%% Reference\r\n% For more information about empty arrays, check out\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/math\/f1-86359.html#f1-86384 this page>\r\n% in the documentation.\r\n%% Got an Empty Question?\r\n% Got any empty questions (really, questions about empty!)?  Or comments?\r\n% Post them <https:\/\/blogs.mathworks.com\/loren\/?p=204#respond here>.\r\n\r\n\r\n##### SOURCE END ##### 99a0320200324b5cac73992d594d08c9\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      MATLAB has had empty arrays since before I started using the program. When I started, the only size empty array was 0x0.  When version 5 was released, empty arrays came along for the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/11\/04\/calculus-with-empty-arrays\/\">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,11,12],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/204"}],"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=204"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/204\/revisions"}],"predecessor-version":[{"id":2574,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/204\/revisions\/2574"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}