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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With