{"id":78,"date":"2007-02-21T10:19:44","date_gmt":"2007-02-21T15:19:44","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=78"},"modified":"2007-04-05T06:00:01","modified_gmt":"2007-04-05T11:00:01","slug":"beginner-woes","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/02\/21\/beginner-woes\/","title":{"rendered":"Beginner Woes"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>There are a few pitfalls that new users of MATLAB almost always fall into, depending on their applications.  I thought it\r\n         might be nice to summarize a few of them here, and, I hope, use you the readers to add to the list.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Matrix Multiplication vs. Elementwise<\/a><\/li>\r\n         <li><a href=\"#3\">Transpose for Complex Data<\/a><\/li>\r\n         <li><a href=\"#5\">Other Beginner Woes?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Matrix Multiplication vs. Elementwise<a name=\"1\"><\/a><\/h3>\r\n   <p>From its inception, MATLAB was meant to be a MATrix LABoratory.  Since initially the only datatype was a two-dimensional array,\r\n      <a href=\"https:\/\/www.mathworks.com\/company\/aboutus\/founders\/clevemoler.html\">Cleve<\/a> chose <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/arithmetic-operators.html\"><tt>*<\/tt><\/a> for the matrix multiplication operator.  To multiply two arrays elementwise, use <tt>.*<\/tt> instead.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">A = magic(3)\r\nB = diag([-1 1 -1])\r\nAB = A*B\r\neachAB = A.*B<\/pre><pre style=\"font-style:oblique\">A =\r\n     8     1     6\r\n     3     5     7\r\n     4     9     2\r\nB =\r\n    -1     0     0\r\n     0     1     0\r\n     0     0    -1\r\nAB =\r\n    -8     1    -6\r\n    -3     5    -7\r\n    -4     9    -2\r\neachAB =\r\n    -8     0     0\r\n     0     5     0\r\n     0     0    -2\r\n<\/pre><p>Sometimes new users type <tt>A*B<\/tt> and find it takes a really long time because they are expecting the elementwise output. For a square matrix of order <tt>N<\/tt>, and without accounting for any special characteristics (e.g., if one of the matrices is diagonal, sparse, banded, etc.),\r\n      then matrix multiplication takes\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/78\/elementwiseAndTranspose_eq1503.png\"> <\/p>\r\n   <p>operations whereas <tt>.*<\/tt> takes\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/78\/elementwiseAndTranspose_eq1502.png\"> <\/p>\r\n   <h3>Transpose for Complex Data<a name=\"3\"><\/a><\/h3>\r\n   <p>Again, going back to MATLAB's roots, the original basic datatype was not only two-dimensional, but the values were double\r\n      and complex (with appropriate storage optimizations if the data were real-valued).  Being the MATrix LABoratory that MATLAB\r\n      is, we needed an operator for transposing a matrix and chose <tt>'<\/tt>.  Looking at the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/arithmetic-operators.html\">documentation for <tt>'<\/tt><\/a>, we see that <tt>'<\/tt> performs the complex conjugate transpose.  We also see that <tt>.'<\/tt> performs the transpose without doing the conjugation.  For real matrices, <tt>'<\/tt> and <tt>.'<\/tt> are the same.   But for complex matrices, they differ.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">Ai = A+i*[1 2 3; 4 5 6; 7 8 9]\r\nAict = Ai'\r\nAit = Ai.'<\/pre><pre style=\"font-style:oblique\">Ai =\r\n   8.0000 + 1.0000i   1.0000 + 2.0000i   6.0000 + 3.0000i\r\n   3.0000 + 4.0000i   5.0000 + 5.0000i   7.0000 + 6.0000i\r\n   4.0000 + 7.0000i   9.0000 + 8.0000i   2.0000 + 9.0000i\r\nAict =\r\n   8.0000 - 1.0000i   3.0000 - 4.0000i   4.0000 - 7.0000i\r\n   1.0000 - 2.0000i   5.0000 - 5.0000i   9.0000 - 8.0000i\r\n   6.0000 - 3.0000i   7.0000 - 6.0000i   2.0000 - 9.0000i\r\nAit =\r\n   8.0000 + 1.0000i   3.0000 + 4.0000i   4.0000 + 7.0000i\r\n   1.0000 + 2.0000i   5.0000 + 5.0000i   9.0000 + 8.0000i\r\n   6.0000 + 3.0000i   7.0000 + 6.0000i   2.0000 + 9.0000i\r\n<\/pre><p>Why does this trip people?  Sometimes when I write a general algorithm, I expect it to work regardless of the data, be it\r\n      real or complex.  Now suppose my code is working with column vectors, but I am willing, as a convenience, to have the code\r\n      handle rows as well.  I just need to transform them, in this case transpose.  If I am not careful here, and use <tt>'<\/tt>, I will conjugate the original data and I didn't mean to.\r\n   <\/p>\r\n   <h3>Other Beginner Woes?<a name=\"5\"><\/a><\/h3>\r\n   <p>I have a few more topics in the Woes category that I will cover in a later blog.  Some of them happen to new users, and some\r\n      happen to even those of us with more MATLAB experience.  Please post other common pitfalls you've seen new users experience\r\n      <a href=\"?p=78#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_f1785603153b47b3ad04de87026f5e41() {\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='f1785603153b47b3ad04de87026f5e41 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' f1785603153b47b3ad04de87026f5e41';\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_f1785603153b47b3ad04de87026f5e41()\"><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\nf1785603153b47b3ad04de87026f5e41 ##### SOURCE BEGIN #####\r\n%% Beginner Woes\r\n% There are a few pitfalls that new users of MATLAB almost always fall\r\n% into, depending on their applications.  I thought it might be nice to\r\n% summarize a few of them here, and, I hope, use you the readers to add to\r\n% the list.\r\n%% Matrix Multiplication vs. Elementwise\r\n% From its inception, MATLAB was meant to be a MATrix LABoratory.  Since\r\n% initially the only datatype was a two-dimensional array,\r\n% <https:\/\/www.mathworks.com\/company\/aboutus\/founders\/clevemoler.html Cleve>\r\n% chose <https:\/\/www.mathworks.com\/help\/matlab\/arithmetic-operators.html |*|>\r\n% for the matrix multiplication operator.  To multiply two arrays\r\n% elementwise, use |.*| instead.\r\nA = magic(3)\r\nB = diag([-1 1 -1])\r\nAB = A*B\r\neachAB = A.*B\r\n%%\r\n% Sometimes new users type |A*B| and find it takes a really long time\r\n% because they are expecting the elementwise output. For a square matrix of\r\n% order |N|, and without accounting for any special characteristics (e.g.,\r\n% if one of the matrices is diagonal, sparse, banded, etc.), then matrix\r\n% multiplication takes \r\n%\r\n% $$O(N^{3})$$ \r\n%\r\n% operations whereas |.*| takes\r\n%\r\n% $$O(N^{2})$$\r\n%% Transpose for Complex Data\r\n% Again, going back to MATLAB's roots, the original basic datatype was not\r\n% only two-dimensional, but the values were double and complex (with\r\n% appropriate storage optimizations if the data were real-valued).  Being\r\n% the MATrix LABoratory that MATLAB is, we needed an operator for\r\n% transposing a matrix and chose |'|.  Looking at the \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/arithmetic-operators.html documentation for |'|>,\r\n% we see that |'| performs the complex conjugate transpose.  We also see\r\n% that |.'| performs the transpose without doing the conjugation.  For real\r\n% matrices, |'| and |.'| are the same.   But for complex matrices, they\r\n% differ.\r\nAi = A+i*[1 2 3; 4 5 6; 7 8 9]\r\nAict = Ai'\r\nAit = Ai.'\r\n%%\r\n% Why does this trip people?  Sometimes when I write a general algorithm, I\r\n% expect it to work regardless of the data, be it real or complex.  Now\r\n% suppose my code is working with column vectors, but I am willing, as a\r\n% convenience, to have the code handle rows as well.  I just need to\r\n% transform them, in this case transpose.  If I am not careful here, and\r\n% use |'|, I will conjugate the original data and I didn't mean to.\r\n\r\n%% Other Beginner Woes?\r\n% I have a few more topics in the Woes category that I will cover in a\r\n% later blog.  Some of them happen to new users, and some happen to even\r\n% those of us with more MATLAB experience.  Please post other common \r\n% pitfalls you've seen new users experience <?p=78#respond here>.\r\n##### SOURCE END ##### f1785603153b47b3ad04de87026f5e41\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      There are a few pitfalls that new users of MATLAB almost always fall into, depending on their applications.  I thought it\r\n         might be nice to summarize a few of them here,... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/02\/21\/beginner-woes\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/78"}],"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=78"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}