I currently have a matrix output from a program that looks like the following where the bottom left has all 1s:
B C D E
A 0 1 2 3
B 1 1 3 3
C 1 1 1 3
D 1 1 1 0
Is there a way to convert it into a symmetrical matrix instead of having all the 1s?
I currently have a matrix output from a program that looks like the following where the bottom left has all 1s:
B C D E
A 0 1 2 3
B 1 1 3 3
C 1 1 1 3
D 1 1 1 0
Is there a way to convert it into a symmetrical matrix instead of having all the 1s?
I do not think that the solution of @RonakShah is correct.
M = matrix(1:16, nrow=4)
M
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
M[lower.tri(M)] <- M[upper.tri(M)]
M
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 5 6 10 14
[3,] 9 13 11 15
[4,] 10 14 15 16
This is not symmetric. Instead, use
M = matrix(1:16, nrow=4)
M[lower.tri(M)] <- t(M)[lower.tri(M)]
M
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 5 6 10 14
[3,] 9 10 11 15
[4,] 13 14 15 16
You can copy the upper triangular values to lower triangle.
mat[lower.tri(mat)] <- mat[upper.tri(mat)]
mat
# B C D E
#A 0 1 2 3
#B 1 1 3 3
#C 2 3 1 3
#D 3 3 3 0