how do i fill the entries of this matrix in R?
Given:
h_i=t_(i+1)-t_i for i=1,...,n-1 where n is a positive integer.
The matrix Q
is an n
by (n-2)
matrix with e开发者_运维百科ntries q_(i,j)
with i=1,...,n
and j=2,...n-1
given by:
q_(j-1,j)=1/h_(j-1)
q_(j,j)=-(1/h_(j-1)+1/h_j)
q_(j+1,j)=1/h_j
q_(i,j)=0 for |i-j|>=2
I want to get a matrix Q
. How do i write a program for this matrix in R? Many thanx in advance.
If I've figured out the subscripts correctly, I think this will do it:
n <- 100
t <- sort(runif(n))
h <- diff(t)
Q <- matrix(0,n,n-2)
Q[row(Q)==col(Q)-1] <- 1/h[1:(n-3)]
Q[row(Q)==col(Q)+1] <- 1/h[1:(n-2)]
diag(Q) <- c(NA,-1/h[1:(n-3)] - 1/h[2:(n-2)])
精彩评论