Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a 4x4 symmetric matrix that is obtained by solving some equations. I tried deleting duplicates with DeleteDuplicates, but that's not working with nested lists (elements of those lists that are the same).

So I have this

$$\begin{pmatrix} a_{11} & a_{12} & a_{13} & a_{14} \\ a_{21} & a_{22} & a_{23} & a_{24} \\ a_{31} & a_{32} & a_{33} & a_{34} \\ a_{41} & a_{42} & a_{43} & a_{44} \\ \end{pmatrix}\to a_{nm}=a_{mn}\to \begin{pmatrix} a_{11} & a_{12} & a_{13} & a_{14} \\ 0 & a_{22} & a_{23} & a_{24} \\ 0 & 0 & a_{33} & a_{34} \\ 0 & 0 & 0 & a_{44} \\ \end{pmatrix}$$

I'd like to be able to remove lower triangle of a matrix but only if they are the same. How to do that?

share|improve this question
I noticed the UpperTriangularize function, which is what I need, it's just that I would like to be sure first if the matrix in question is symmetric. Should I make an If statement? – dingo_d Oct 14 '13 at 16:12
You can use SymmetricMatrixQ – R. M. Oct 14 '13 at 16:22
up vote 1 down vote accepted

Let mat be your matrix:

func[mat_?SymmetricMatrixQ] := UpperTriangularize[mat];
func[mat_?(Not@SymmetricMatrixQ[#] &)] := mat;

If the matrix is symmetric the above will convert to upper triangular matrix including diagonal,else it will return original matrix, e.g.

m = {{a, b, c}, {b, d, e}, {c, e, f}};


enter image description here

share|improve this answer
Thanks :) That does the trick :) – dingo_d Oct 15 '13 at 12:49

I don't think this applies specifically to the OPs situation, but I thought I'd contribute just in case it's useful. As mentioned in the comments, using UpperTriangularize with a conditional statement to check for symmetry is the appropriate route; however if the matrix elements are, for example, coordinates, then SymmetricMatrixQ won't recognize the matrix as symmetric. A circuitous route to a 4x4 matrix of x,y coordinates:

m1 =  RandomInteger[{1, 10}, {4, 4}];
m2 =  RandomInteger[{1, 10}, {4, 4}];
sm1 = m1 + Transpose[m1];
sm2 = m2 + Transpose[m2];
MapThread[Riffle[#1, #2] &, {sm1, sm2}];
sm3 = Partition[#, 2] & /@ %;

Mathematica graphics

SymmetricMatrixQ[sm3] returns False, but sm3 == Tranpose[sm3] returns True. Additionally, UpperTriangularize doesn't like this matrix, so I make my own function:

newUpperTriangularize[x_] := UpperTriangularize[x /. {_, _} -> 1]*x;
newUpperTriangularize[sm3] // MatrixForm

Mathematica graphics

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.