Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert number above the diagonal in R matrix

Tags:

r

diagonal

I have a vector like this in R:

vec1 <- c(14000,12000,8000)

I'm trying to create a matrix where 14000 is my main diagonal, 1200 is one above the diagonal, 8000 two above the diagonal.

I'm familiar with doing this in Python/numpy but can't figure it out in R (or at least an efficient way to do it). Ideally output would look like this:

14000 12000  8000
    0 14000 12000
    0     0 14000
like image 811
itjcms18 Avatar asked Apr 23 '15 16:04

itjcms18


2 Answers

Try

m1 <- t(matrix(vec1, nrow=5, ncol=3))[,1:3]
m1[lower.tri(m1)] <- 0
m1
#     [,1]  [,2]  [,3]
#[1,] 14000 12000  8000
#[2,]     0 14000 12000
#[3,]     0     0 14000

or use toeplitz

toeplitz(vec1)*upper.tri(diag(seq_along(vec1)), diag=TRUE)
#      [,1]  [,2]  [,3]
#[1,] 14000 12000  8000
#[2,]     0 14000 12000
#[3,]     0     0 14000

Or a modification suggested by @David Arenburg

m <- toeplitz(vec1)
m[lower.tri(m)] <- 0
like image 177
akrun Avatar answered Sep 18 '22 13:09

akrun


In this simple case you can assign the diagonal and upper triangular portions separately like so:

m <- matrix(0, nrow=3, ncol=3)

diag(m) <- 14000

m[upper.tri(m)] <- c(12000, 8000, 12000)

However, as David Arenburg pointed out, this is a more manual approach and thus doesn't scale well. For a more automated, scalable approach, I recommend akrun's solution using toeplitz() and lower.tri().

I'll keep this answer here for the sake of completeness in your specific case, but I think that akrun's is the better general solution.

like image 37
Alex A. Avatar answered Sep 20 '22 13:09

Alex A.