[Solved] Efficient way to create matrix of different value of a vector in R [closed]


I’m a bit lost on the question, but this line

vec <- 1:3
embed(c(rep(0,length(vec)),vec),length(vec)+1)

…produces the last part of your desired result:

     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    2    1    0    0
[3,]    3    2    1    0

Which you can then bind to the first number of the vector.

cbind(vec[1],embed(c(rep(0,length(vec)),vec),length(vec)+1))

to give…

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    0    0    0
[2,]    1    2    1    0    0
[3,]    1    3    2    1    0

1

solved Efficient way to create matrix of different value of a vector in R [closed]