Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
577 views
in Technique[技术] by (71.8m points)

r - Make a matrix symmetric

I have a matrix that should be symmetric according to theory, but might not be observed as symmetric in my data. I would like to force this to be symmetric by using the maximum of the two compared cells.

test_matrix <- matrix(c(0,1,2,1,0,1,1.5,1,0), nrow = 3)
test_matrix
#>     [,1] [,2] [,3]
#>[1,]    0    1  1.5
#>[2,]    1    0  1.0
#>[3,]    2    1  0.0

It's easy enough to do this with a double loop.

for(i in 1:3){
  for(j in 1:3){
    test_matrix[i, j] <- max(test_matrix[i, j], test_matrix[j, i]) 
   }
}
test_matrix
#>      [,1] [,2] [,3]
#> [1,]    0    1    2
#> [2,]    1    0    1
#> [3,]    2    1    0

But my matrix is larger than $3x3$, and R's problems with loops are well-documented. I'm also interested in making my code as clean as possible. In fact, I considered putting this on code golf, but this is a real problem that I think others might be interested in.

I've seen this one as well as this one, but mine is different in that those op's seemed to actually have a symmetric matrix that just needed reordering, and I have a matrix that I need to change to be symmetric.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could use pmax(), which returns the element-wise maxima of a pair of vectors.

pmax(test_matrix, t(test_matrix))
#      [,1] [,2] [,3]
# [1,]    0    1    2
# [2,]    1    0    1
# [3,]    2    1    0

It'll work with a pair of matrices, as here, because: (1) in R, matrices are 'just' vectors with attached (dimension) attributes; and (2) the code used to implement pmax() is nice enough to reattach the attributes of it's first argument to the value that it returns.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...