Search code examples
rmatrixfilldiagonal

How to fill in a matrix given diagonal and off-diagonal elements in r?


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!


Solution

  • 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