7
$\begingroup$

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.

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

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]]];
$\endgroup$
  • $\begingroup$ Yes...That is what I wanted to have. Perfect... $\endgroup$ – Tugrul Temel Oct 28 '18 at 14:52
  • 1
    $\begingroup$ @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] ! $\endgroup$ – Ulrich Neumann Oct 28 '18 at 15:05
  • 1
    $\begingroup$ @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. $\endgroup$ – Henrik Schumacher Oct 28 '18 at 15:09
  • $\begingroup$ @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. $\endgroup$ – Tugrul Temel Oct 28 '18 at 15:19
6
$\begingroup$

Maybe a bit simpler:

M Transpose[M]

Comparing with Henrik's answer:

M Subtract[1, Unitize[Subtract[Transpose[M], M]]] == M Transpose[M]
True
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Henrik Schumacher Oct 28 '18 at 16:19
  • $\begingroup$ @Henrik -- Agreed, I took the binary example suggested by the OP as indicating interest only in binary matrices. $\endgroup$ – bill s Oct 28 '18 at 16:25
  • $\begingroup$ @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. $\endgroup$ – Tugrul Temel Oct 28 '18 at 16:37
  • 1
    $\begingroup$ 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. $\endgroup$ – Henrik Schumacher Oct 28 '18 at 16:38
  • $\begingroup$ @Henrik: I will edit the question in line with your comment. $\endgroup$ – Tugrul Temel Oct 28 '18 at 17:47
4
$\begingroup$
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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Tugrul Temel Oct 28 '18 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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