{"id":2377,"date":"2016-10-27T15:43:02","date_gmt":"2016-10-27T19:43:02","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=2377"},"modified":"2019-11-01T16:50:40","modified_gmt":"2019-11-01T20:50:40","slug":"matlab-arithmetic-expands-in-r2016b","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2016\/10\/27\/matlab-arithmetic-expands-in-r2016b\/","title":{"rendered":"MATLAB arithmetic expands in R2016b"},"content":{"rendered":"<div class=\"content\"><p>Earlier this summer, I was writing some color-space conversion code. At one point in the code, I had a Px3 matrix called <tt>RGB<\/tt>, which contained P colors, one per row. I also had a 1x3 vector, <tt>v<\/tt>. I needed to multiply each column of <tt>RGB<\/tt> by the corresponding element of <tt>v<\/tt>, like this:<\/p><pre>RGB_c = [RGB(:,1)*v(1)  RGB(:,2)*v(2)  RGB(:,3)*v(3)];<\/pre><p>But since I was using an internal developer build of MATLAB R2016 (released on September 14), I didn't type the code above. Instead, I typed this:<\/p><pre>RGB_c = RGB .* v;<\/pre><p>In R2016a and older MATLAB releases, that line of code produced an error:<\/p><p>\r\n<pre>\r\n>> RGB_c = RGB .* v\r\n<span style=\"color: rgb(230,0,0);\">Error using <span style=\"text-decoration: underline;\"> .* <\/span><\/span>\r\n<span style=\"color: rgb(230,0,0);\">Matrix dimensions must agree.<\/span>\r\n<\/pre>\r\n<\/p><p>In the new release, though, MATLAB <b>implicitly expands<\/b> the vector <tt>v<\/tt> to be the same size as the matrix <tt>RGB<\/tt> and then carries out the elementwise multiplication. I say \"implicitly\" because MATLAB does not actually make an in-memory copy of the expanded vector.<\/p><p>To read all about this change in MATLAB matrix arithmetic, head over to <a href=\"https:\/\/blogs.mathworks.com\/loren\">Loren's Art of MATLAB blog<\/a> and read my <a href=\"https:\/\/blogs.mathworks.com\/loren\/2016\/10\/24\/matlab-arithmetic-expands-in-r2016b\/\">guest post<\/a> about it.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_0ce5e9e0a7784a8092c9fe8eb5cd4871() {\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='0ce5e9e0a7784a8092c9fe8eb5cd4871 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 0ce5e9e0a7784a8092c9fe8eb5cd4871';\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        copyright = 'Copyright 2016 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_0ce5e9e0a7784a8092c9fe8eb5cd4871()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2016b<br><\/p><\/div><!--\r\n0ce5e9e0a7784a8092c9fe8eb5cd4871 ##### SOURCE BEGIN #####\r\n%% MATLAB arithmetic expands in R2016b\r\n% Earlier this summer, I was writing some color-space conversion code. At\r\n% one point in the code, I had a Px3 matrix called |RGB|, which contained P\r\n% colors, one per row. I also had a 1x3 vector, |v|. I needed to multiply\r\n% each column of |RGB| by the corresponding element of |v|, like this:\r\n%\r\n%  RGB_c = [RGB(:,1)*v(1)  RGB(:,2)*v(2)  RGB(:,3)*v(3)];\r\n%\r\n% But since I was using an internal developer build of MATLAB R2016\r\n% (released on September 14), I didn't type the code above. Instead, I\r\n% typed this:\r\n%\r\n%  RGB_c = RGB .* v;\r\n%\r\n% In R2016a and older MATLAB releases, that line of code produced an error:\r\n%\r\n% <html>\r\n% <pre>\r\n% >> RGB_c = RGB .* v\r\n% <span style=\"color: rgb(230,0,0);\">Error using <span style=\"text-decoration: underline;\"> .* <\/span><\/span>\r\n% <span style=\"color: rgb(230,0,0);\">Matrix dimensions must agree.<\/span>\r\n% <\/pre>\r\n% <\/html>\r\n%\r\n% In the new release, though, MATLAB *implicitly expands* the vector |v| to\r\n% be the same size as the matrix |RGB| and then carries out the elementwise\r\n% multiplication. I say \"implicitly\" because MATLAB does not actually make\r\n% an in-memory copy of the expanded vector.\r\n%\r\n% To read all about this change in MATLAB matrix arithmetic, head\r\n% over to <https:\/\/blogs.mathworks.com\/loren Loren's Art of MATLAB blog> \r\n% and read my <https:\/\/blogs.mathworks.com\/loren\/2016\/10\/24\/matlab-arithmetic-expands-in-r2016b\/ \r\n% guest post> about it.\r\n\r\n##### SOURCE END ##### 0ce5e9e0a7784a8092c9fe8eb5cd4871\r\n-->","protected":false},"excerpt":{"rendered":"<p>Earlier this summer, I was writing some color-space conversion code. At one point in the code, I had a Px3 matrix called RGB, which contained P colors, one per row. I also had a 1x3 vector, v. I... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/10\/27\/matlab-arithmetic-expands-in-r2016b\/\">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":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/2377"}],"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=2377"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/2377\/revisions"}],"predecessor-version":[{"id":2379,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/2377\/revisions\/2379"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=2377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=2377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=2377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}