2

I have the elements for a matrix as follows:

diag= rep(1,5)
offdiag = c(rep(1:4), rep(1:3), rep(1:2), 1)

The final matrix I want should should be a symmetric matrix that looks like this:

1 1 2 3 4
1 1 1 2 3 
2 1 1 1 2
3 2 1 1 1
4 3 2 1 1

where the diagonal is filled by diag and the lower-trianglar area is filled by offdiag column-wise.

In practice, all all numbers are random. So I need a generic way to fill in the matrix with elements.

Thanks in advance!

wen
  • 1,875
  • 4
  • 26
  • 43

1 Answers1

6

Try this:

m <- matrix(NA, ncol = length(diag), nrow = length(diag))
m[lower.tri(m)] <- offdiag
m[upper.tri(m)] <- t(m)[upper.tri(t(m))]
diag(m) <- diag
m
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    1    1    2    3    4
# [2,]    1    1    1    2    3
# [3,]    2    1    1    1    2
# [4,]    3    2    1    1    1
# [5,]    4    3    2    1    1

Another alternative: Manually create a distance matrix and work from there.

class(offdiag) <- "dist"
attr(offdiag, "Size") <- length(diag)
out <- as.matrix(offdiag)
diag(out) <- diag
out
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485