I am trying to fill the lower diagonal of my matrix M with a prefilled vector, V
My original matrix looks like similar to this:
M = matrix(c(.3,.2,.1,0), nrow=4, ncol=5)
M 1 2 3 4 5
1 .3 .3 .3 .3 .3
2 .2 .2 .2 .2 .3
3 .1 .1 .1 .1 .1
4 0 0 0 0 0
I have a vector similar to this:
V
.4
.3
.25
.1
Now I want to fill the lower triangle with this vector, to get:
0 1 2 3 4 5
1 .3 .3 .3 .3 .1
2 .2 .2 .2 .25 .25
3 .1 .1 .3 .3 .3
4 0 .4 .4 .4 .4
If I use the lower.tri
function it gives out an error so I built a loop which only should fill the columns from the buttom up:
o <- 5
c <- 2
s <- 1
for(s in (1:o)){
for(c in (2:o)){
M[((o-s):o),c] <- V[1:c]}}
My idea was to move upwards like I manually wrote:
M[(5-1):5,2] <- V[1:2]
M[(5-2):5,3] <- V[1:3]
What's the best way?