{"id":829,"date":"2014-01-08T09:27:11","date_gmt":"2014-01-08T14:27:11","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=829"},"modified":"2014-01-08T09:27:11","modified_gmt":"2014-01-08T14:27:11","slug":"seeking-symmetry-in-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2014\/01\/08\/seeking-symmetry-in-matlab\/","title":{"rendered":"Seeking Symmetry in MATLAB"},"content":{"rendered":"\r\n<div class=\"content\"><!--introduction--><p>Sometimes I need to construct a matrix with certain symmetries.  There are a  bunch of tools in MATLAB that are well suited for such tasks.  In today's post, I will mention the ones I use most often.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#321f48b6-b116-47db-b444-4063399b718c\">Perhaps the Most Ignored Functionality For Symmetry<\/a><\/li><li><a href=\"#d115f118-1d3b-4808-be58-47999f661dd7\">Vector Symmetry<\/a><\/li><li><a href=\"#8f24681a-d710-41ef-94b2-bc94d46e710d\">Some Other Tools to Help Symmetrize<\/a><\/li><li><a href=\"#6a8d76ac-b659-4290-9653-9d429bd88ea4\">Does Your Work Require Symmetry?<\/a><\/li><\/ul><\/div><h4>Perhaps the Most Ignored Functionality For Symmetry<a name=\"321f48b6-b116-47db-b444-4063399b718c\"><\/a><\/h4><p>I want to start with perhaps the most ignored funtionality for ensuring symmetry of a certain kind is the transpose operators <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/transpose.html\"><tt>.'<\/tt> (<tt>ctranspose<\/tt>)<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/ctranspose.html\"><tt>'<\/tt> (<tt>transpose<\/tt>)<\/a>.  The reason these can be so so helpful is that during the construction of a matrix <tt>A<\/tt>, that we know should be symmetric (such as a covariance matrix) or hermetian (such as a hessian), the corresponding elements <tt>A(i,j)<\/tt> and <tt>A(j,i)<\/tt> may be computed with different expressions, or with the terms being aggregated in different orders.  If that happens, these elements may differ due to numerical considerations. However, if the next step in the algorithm <b>requires<\/b> <tt>A<\/tt> to be symmetric, then the code may fail.  So, to ensure symmetry, assuming <tt>A<\/tt> is real, you can do something like this:<\/p><pre>              A = (A + A')\/2<\/pre><p>Of course, another thing you might do is only calculate the elements on the diagonal and either above or below, and then fill in the remaining elements.  A third, strategy, if you have control of the algorithm following the matrix construction, is to only use the upper or lower triangular elements, reducing the need to symmetrize the input.<\/p><p>Also, if for some reason I was working in higher dimensions and needed symmetry in a \"plane\", I could use <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/permute.html\"><tt>permute<\/tt><\/a> and its companion <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/ipermute.html\"><tt>ipermute<\/tt><\/a>.<\/p><h4>Vector Symmetry<a name=\"d115f118-1d3b-4808-be58-47999f661dd7\"><\/a><\/h4><p>There are other kinds of symmetries that arise in numerical computations. One example is for windowing signals for certain signal processing applications.  In this case, the window that is applied is generally symmetric, but in a different way.  For a window <tt>W<\/tt> of length <tt>n<\/tt>, the symmetry can be expressed like this:<\/p><pre>           W(end:-1:(ceil(n\/2)+1)) = W(1:floor(n\/2))<\/pre><p>NOTE: I was careful in the above expression to not copy the middle element of an odd-length array :-) !<\/p><p>If I had just computed the first half of the window (being careful about odd and even lengths), I could just use one of these functions to get the window symmetry instead of working out the expression above, using the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/colon.html\"><tt>:<\/tt> (<tt>colon<\/tt>)<\/a> operator and some math.<\/p><div><ul><li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/flipud.html\"><tt>flipud<\/tt><\/a> - Flip matrix up to down<\/li><li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fliplr.html\"><tt>fliplr<\/tt><\/a> - Flip matrix left to right<\/li><li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/flipdim.html\"><tt>flipdim<\/tt><\/a> - Flip array along specified dimension<\/li><\/ul><\/div><h4>Some Other Tools to Help Symmetrize<a name=\"8f24681a-d710-41ef-94b2-bc94d46e710d\"><\/a><\/h4><p>Here's a list (likely not exhaustive) of other tools to help produce symmetric arrays.<\/p><div><ul><li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/rot90.html\"><tt>rot90<\/tt><\/a> - Rotate matrix 90 degrees<\/li><li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/circshift.html\"><tt>circshift<\/tt><\/a> -Shift array circularly<\/li><\/ul><\/div><div><ul><li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/diag.html\"><tt>diag<\/tt><\/a> - Diagonal matrices and diagonals of matrix<\/li><li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/tril.html\"><tt>tril<\/tt><\/a> - Lower triangular part of matrix<\/li><li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/triu.html\"><tt>triu<\/tt><\/a> - Upper triangular part of matrix<\/li><\/ul><\/div><div><ul><li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/reshape.html\"><tt>reshape<\/tt><\/a> - Reshape array<\/li><li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/shiftdim.html\"><tt>shiftdim<\/tt><\/a> - Shift dimensions<\/li><\/ul><\/div><div><ul><li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/repmat.html\"><tt>repmat<\/tt><\/a> - Replicate and tile array<\/li><li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/kron.html\"><tt>kron<\/tt><\/a> - Kronecker tensor product<\/li><\/ul><\/div><p>And here's one I basically never use, because I find that I have to do a lot of special case coding if I do.<\/p><div><ul><li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/squeeze.html\"><tt>squeeze<\/tt><\/a> - Remove singleton dimensions<\/li><\/ul><\/div><h4>Does Your Work Require Symmetry?<a name=\"6a8d76ac-b659-4290-9653-9d429bd88ea4\"><\/a><\/h4><p>When do you need and use symmetry?  In what application?  And how do you achieve that in your code.  Let us know <a href=\"blogs.mathworks.com\/loren\/?p=829#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_4c382fb084df46889ff6f1dda2bf7672() {\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='4c382fb084df46889ff6f1dda2bf7672 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 4c382fb084df46889ff6f1dda2bf7672';\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 2014 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_4c382fb084df46889ff6f1dda2bf7672()\"><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; R2013b<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2013b<br><\/p><\/div><!--\r\n4c382fb084df46889ff6f1dda2bf7672 ##### SOURCE BEGIN #####\r\n%% Seeking Symmetry in MATLAB\r\n% Sometimes I need to construct a matrix with certain symmetries.  There\r\n% are a  bunch of tools in MATLAB that are well suited for such tasks.  In\r\n% today's post, I will mention the ones I use most often.\r\n\r\n%% Perhaps the Most Ignored Functionality For Symmetry\r\n% I want to start with perhaps the most ignored funtionality for ensuring\r\n% symmetry of a certain kind is the transpose operators\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/transpose.html |.'|\r\n% (|ctranspose|)> and\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/ctranspose.html |'|\r\n% (|transpose|)>.  The reason these can be so so helpful is that during the\r\n% construction of a matrix |A|, that we know should be symmetric (such as a\r\n% covariance matrix) or hermetian (such as a hessian), the corresponding\r\n% elements |A(i,j)| and |A(j,i)| may be computed with different\r\n% expressions, or with the terms being aggregated in different orders.  If\r\n% that happens, these elements may differ due to numerical considerations.\r\n% However, if the next step in the algorithm *requires* |A| to be\r\n% symmetric, then the code may fail.  So, to ensure symmetry, assuming |A|\r\n% is real, you can do something like this:\r\n%\r\n%                A = (A + A')\/2\r\n%\r\n% Of course, another thing you might do is only calculate the elements on\r\n% the diagonal and either above or below, and then fill in the remaining\r\n% elements.  A third, strategy, if you have control of the algorithm\r\n% following the matrix construction, is to only use the upper or lower\r\n% triangular elements, reducing the need to symmetrize the input.\r\n%\r\n% Also, if for some reason I was working in higher dimensions and needed\r\n% symmetry in a \"plane\", I could use\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/permute.html |permute|> and its\r\n% companion <https:\/\/www.mathworks.com\/help\/matlab\/ref\/ipermute.html\r\n% |ipermute|>.\r\n\r\n%% Vector Symmetry\r\n% There are other kinds of symmetries that arise in numerical computations.\r\n% One example is for windowing signals for certain signal processing\r\n% applications.  In this case, the window that is applied is generally\r\n% symmetric, but in a different way.  For a window |W| of length |n|, the\r\n% symmetry can be expressed like this:\r\n%\r\n%             W(end:-1:(ceil(n\/2)+1)) = W(1:floor(n\/2))\r\n\r\n%%\r\n% NOTE: I was careful in the above expression to not copy the middle\r\n% element of an odd-length array :-) !\r\n%\r\n% If I had just computed the first half of the window (being careful about\r\n% odd and even lengths), I could just use one of these functions to get the\r\n% window symmetry instead of working out the expression above, using the\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/colon.html |:| (|colon|)>\r\n% operator and some math.\r\n%\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/flipud.html |flipud|> - Flip\r\n% matrix up to down\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/fliplr.html |fliplr|> - Flip\r\n% matrix left to right\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/flipdim.html |flipdim|> -\r\n% Flip array along specified dimension\r\n\r\n%% Some Other Tools to Help Symmetrize\r\n% Here's a list (likely not exhaustive) of other tools to help produce\r\n% symmetric arrays.\r\n%\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/rot90.html |rot90|> - Rotate\r\n% matrix 90 degrees\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/circshift.html |circshift|>\r\n% -Shift array circularly\r\n% \r\n%\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/diag.html |diag|> - Diagonal\r\n% matrices and diagonals of matrix\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/tril.html |tril|> - Lower\r\n% triangular part of matrix\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/triu.html |triu|> - Upper\r\n% triangular part of matrix\r\n%\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/reshape.html |reshape|> -\r\n% Reshape array\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/shiftdim.html |shiftdim|> -\r\n% Shift dimensions\r\n%\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/repmat.html |repmat|> -\r\n% Replicate and tile array\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/kron.html |kron|> - Kronecker\r\n% tensor product\r\n%\r\n% And here's one I basically never use, because I find that I have to do a\r\n% lot of special case coding if I do.\r\n%\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/squeeze.html |squeeze|> -\r\n% Remove singleton dimensions\r\n%\r\n%% Does Your Work Require Symmetry?\r\n% When do you need and use symmetry?  In what application?  And how do you\r\n% achieve that in your code.  Let us know\r\n% <blogs.mathworks.com\/loren\/?p=829#respond here>.\r\n\r\n\r\n##### SOURCE END ##### 4c382fb084df46889ff6f1dda2bf7672\r\n-->","protected":false},"excerpt":{"rendered":"<!--introduction--><p>Sometimes I need to construct a matrix with certain symmetries.  There are a  bunch of tools in MATLAB that are well suited for such tasks.  In today's post, I will mention the ones I use most often.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2014\/01\/08\/seeking-symmetry-in-matlab\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[39,26],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/829"}],"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=829"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/829\/revisions"}],"predecessor-version":[{"id":832,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/829\/revisions\/832"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=829"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}