# Extract the symmetric matrix built-in another matrix

Suppose that I have a matrix M:

M={
{0, 1, 1, 0, 1, 0},
{1, 0, 0, 1, 1, 1},
{1, 1, 0, 0, 1, 0},
{0, 1, 1, 0, 0, 0},
{1, 0, 0, 1, 0, 1},
{1, 1, 1, 1, 0, 0}
};


I like to extract from M the symmetric matrix symM:

symM={
{0, 1, 1, 0, 1, 0},
{1, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0}
};


I do not want to use Do or If commands. I like to implement matrix operations to extract the symmetric part of M.

EDIT 1

In general terms, matrix M is composed of 1 and 0 only, with the condition that the diagonal cells be zeros. My goal is to extract the matrix symM which should only include 1s in the non-zero reciprocal cells (or symmetric cells), otherwise zero.

Example, in the above example, M[[1,2]]=1 and M[[2,1]]=1, then both symM[[1,2]] and symM[[2,1]] should be 1. All other cells which are not qualified should be all zero.

I hope the question is clearer now. Thank you.

• Do you have a particular algorithm in mind? – corey979 Oct 28 '18 at 14:36
• @corey979: I do not have any algorithm in mind but maybe Scan can be used to collect the non-negative symmetric positions in M. – Tugrul Temel Oct 28 '18 at 14:38
• BitAnd[M, Transpose[M]] – Coolwater Oct 28 '18 at 16:41
• @Coolwater That's a good one! Why don't you post it as an answer? – Henrik Schumacher Oct 28 '18 at 17:06

Maybe this is what you are looking for. I interpreted your question as if you want to replace all nonsymmetric entries of the input matrix by zeroes and as if the input matrix is not necessarily binary.

A = M Subtract[1, Unitize[Subtract[Transpose[M], M]]];

• Yes...That is what I wanted to have. Perfect... – Tugrul Temel Oct 28 '18 at 14:52
• @HenrikSchumacher For the symmetric part of M I would have expected something like (M+Transpose[M])/2...Interestingly your result fullfills A==Floor[(M+Transpose[M])/2] ! – Ulrich Neumann Oct 28 '18 at 15:05
• @UlrichNeumann (M+Transpose[M])/2 is the symmetrized part, and in particular the orthogonal projection onto the linear space of symmetric matrices (orthogonal with respect to the Frobenius metric). But as by OP's examples, this was not what the OP asked for. – Henrik Schumacher Oct 28 '18 at 15:09
• @Henrik: Referring to your last point, you are right that your code gives me what I wanted. I checked it with other examples and it works. – Tugrul Temel Oct 28 '18 at 15:19

Maybe a bit simpler:

M Transpose[M]


M Subtract[1, Unitize[Subtract[Transpose[M], M]]] == M Transpose[M]
True

• Simpler indeed. But it will work only for binary matrices. Well, actually, it is still somewhat unclear, what the OP's usage spectrum and desired result in the general case is supposed to be. I interpreted it as if the OP wants to replace all nonsymmetric entries of the input matrix by zeroes. – Henrik Schumacher Oct 28 '18 at 16:19
• @Henrik -- Agreed, I took the binary example suggested by the OP as indicating interest only in binary matrices. – bill s Oct 28 '18 at 16:25
• @Henrik: I wanted to extract binary cells only: that is, if (i,j)==(j,i)==1, then keep them in the output matrix, otherwise place zeros to both cells. What you are saying in your comment is what I actually wanted: replace all non-symmetric entries of the input matrix with zeros. Is Bill's proposal equivalent to your proposal? I want to make sure this because in the original case with n=1000 I cannot check it visually. – Tugrul Temel Oct 28 '18 at 16:37
• A simple example for which bill's and my proposals lead to different output is the matrix M = {{1, 2},{1,1}}. In general it is preferrable to post also all assumptions on the input data already in the question. Moreover your question leave a lot room for interpretation. Please specify exactly what the desired output for a general input matrix should be. – Henrik Schumacher Oct 28 '18 at 16:38
• @Henrik: I will edit the question in line with your comment. – Tugrul Temel Oct 28 '18 at 17:47
A = Floor @ Symmetrize @ M;

TeXForm @ MatrixForm @ A


$$\left( \begin{array}{cccccc} 0 & 1 & 1 & 0 & 1 & 0 \\ 1 & 0 & 0 & 1 & 0 & 1 \\ 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 \\ 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 \\ \end{array} \right)$$

Note: This is much slower than the methods in the answers by bill s and Henrik Schumacher.

• Your answer showed me a mistake in my symM in the original post. Somehow the symM matrix was not symmetric (I do not know how I did this typo). but now the symM is correct, thanks to your post. – Tugrul Temel Oct 28 '18 at 19:37