## Loren on the Art of MATLABTurn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

# Seeking Symmetry in MATLAB

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.

### Contents

#### Perhaps the Most Ignored Functionality For Symmetry

I want to start with perhaps the most ignored funtionality for ensuring symmetry of a certain kind is the transpose operators .' (ctranspose) and ' (transpose). The reason these can be so so helpful is that during the construction of a matrix A, that we know should be symmetric (such as a covariance matrix) or hermetian (such as a hessian), the corresponding elements A(i,j) and A(j,i) 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 requires A to be symmetric, then the code may fail. So, to ensure symmetry, assuming A is real, you can do something like this:

              A = (A + A')/2

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.

Also, if for some reason I was working in higher dimensions and needed symmetry in a "plane", I could use permute and its companion ipermute.

#### Vector Symmetry

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 W of length n, the symmetry can be expressed like this:

           W(end:-1:(ceil(n/2)+1)) = W(1:floor(n/2))

NOTE: I was careful in the above expression to not copy the middle element of an odd-length array :-) !

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 : (colon) operator and some math.

• flipud - Flip matrix up to down
• fliplr - Flip matrix left to right
• flipdim - Flip array along specified dimension

#### Some Other Tools to Help Symmetrize

Here's a list (likely not exhaustive) of other tools to help produce symmetric arrays.

• diag - Diagonal matrices and diagonals of matrix
• tril - Lower triangular part of matrix
• triu - Upper triangular part of matrix
• repmat - Replicate and tile array
• kron - Kronecker tensor product

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.

• squeeze - Remove singleton dimensions

#### Does Your Work Require Symmetry?

When do you need and use symmetry? In what application? And how do you achieve that in your code. Let us know here.

Published with MATLAB® R2013b