Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
1  
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
4  
You can use SymmetricMatrixQ –  rm -rf Oct 14 '13 at 16:22
add comment

2 Answers

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

yields:

enter image description here

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

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] & /@ %;
MatrixForm[sm3]

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
add comment

Your Answer

 
discard

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.