# Deleting duplicates from matrix

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?

-
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

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}};
m//MatrixForm
func[m]//MatrixForm


yields:

-
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];

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;