如何在R中填写此矩阵的条目?

时间:2022-04-12 21:25:39

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 entries q_(i,j) with i=1,...,n and j=2,...n-1 given by:

矩阵Q是n×(n-2)矩阵,条目q_(i,j),其中i = 1,...,n和j = 2,... n-1由下式给出:

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.

我想得到一个矩阵Q.我如何在R中为这个矩阵编写一个程序?很多人提前。

1 个解决方案

#1


1  

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)])

#1


1  

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)])