Create a symmetric matrix in Mathematica

Here is a quick way to create a symmetric matrix in Mathematica. The code below creates a generic 6×6 matrix (line 1), then mirrors the above-diagonal elements to their below-diagonal counterparts (substitution in lines 3-5).

am = Array[Subscript[a,##] &, {6,6}];
am = am /.
  Flatten[Table[
    Subscript[a,i,j] -> Subscript[a,j,i],
    {i,2,6}, {j,1,i-1}]]
This entry was posted in Scientific Computing, Tips & Tricks and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>