I have a 70x70 numpy ndarray, which is mainly diagonal. The only off-diagonal values are the below the diagonal. I would like to make the matrix symmetric.
As a newcomer from Matlab world, I can't get it working without for loops. In MATLAB it was easy:
W = max(A,A')
where A'
is matrix transposition and the max()
function takes care to make the W matrix which will be symmetric.
Is there an elegant way to do so in Python as well?
EXAMPLE
The sample A
matrix is:
1 0 0 0
0 2 0 0
1 0 2 0
0 1 0 3
The desired output matrix W
is:
1 0 1 0
0 2 0 1
1 0 2 0
0 1 0 3
Given an integer N and a N x N matrix, the task is to convert the given matrix into a symmetric matrix by replacing (i, j)th and (j, i)th element with their arithmetic mean. Explanation: The diagonal elements are same.
Use the NumPy tril and triu functions as follows. It essentially "mirrors" elements in the lower triangle into the upper triangle. tril(m, k=0) gets the lower triangle of a matrix m (returns a copy of the matrix m with all elements above the k th diagonal zeroed).
An Efficient solution to check a matrix is symmetric or not is to compare matrix elements without creating a transpose. We basically need to compare mat[i][j] with mat[j][i].
Found a following solution which works for me:
import numpy as np
W = np.maximum( A, A.transpose() )
Use the NumPy tril
and triu
functions as follows. It essentially "mirrors" elements in the lower triangle into the upper triangle.
import numpy as np
A = np.array([[1, 0, 0, 0], [0, 2, 0, 0], [1, 0, 2, 0], [0, 1, 0, 3]])
W = np.tril(A) + np.triu(A.T, 1)
tril(m, k=0)
gets the lower triangle of a matrix m
(returns a copy of the matrix m
with all elements above the k
th diagonal zeroed). Similarly, triu(m, k=0)
gets the upper triangle of a matrix m
(all elements below the k
th diagonal zeroed).
To prevent the diagonal being added twice, one must exclude the diagonal from one of the triangles, using either np.tril(A) + np.triu(A.T, 1)
or np.tril(A, -1) + np.triu(A.T)
.
Also note that this behaves slightly differently to using maximum
. All elements in the upper triangle are overwritten, regardless of whether they are the maximum or not. This means they can be any value (e.g. nan
or inf
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With