R中的广义矩阵(用于求解线性方程组)

R中的广义矩阵(用于求解线性方程组),r,matrix,system,R,Matrix,System,我需要用R来解一个线性方程组,我已经做得很好了。请参阅下面的代码: A<-matrix(c(1:5,2,1,2:4,3,2,1:3,4:2,1,2,5:1),nrow=5) #Creates a matrix of the coefficients A #Displays the matrix of coefficients (below) [,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 [2,] 2 1

我需要用R来解一个线性方程组,我已经做得很好了。请参阅下面的代码:

A<-matrix(c(1:5,2,1,2:4,3,2,1:3,4:2,1,2,5:1),nrow=5) #Creates a matrix of the coefficients
A #Displays the matrix of coefficients (below)

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

kv<-matrix(c(7,-1,-3,5,17),nrow=5) #Creates a column vector of the known values

kv #Displays the column vector

     [,1]
[1,]    7
[2,]   -1
[3,]   -3
[4,]    5
[5,]   17

solve(A,kv) #Solves the continuous equation

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

A如果我理解正确:

bandmat <- function(n){
require(Matrix)
as.matrix(bandSparse(n,n,-(n-1):(n-1),diag=lapply(c(n:1,2:n),function(i) rep(i,n))))
}

bandmat(4)

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

bandmat-Ummm。。。首先,矩阵系数的来源是什么?例如,有人向您发送一组文本字符串,如
k*y1=a*x1+b*x2+c*x3
,您想提取值“k,a,b,c”?您好,谢谢您的回复。这就是我想要的,但它需要推广,所以我需要它来适应任何大小的矩阵,而不是指定bandmat(4)——这可能吗?@TanyaKiddle,我不明白。你可以用这种任意大小的能带结构计算矩阵。试一下bandmat(10)
,非常感谢;对不起,我误解了。我现在需要解连续方程。我有代码来显示列向量kvsolve(A,kv)这现在不起作用了。你能建议我如何做>solve(A,kv)的等价物吗合并你的矩阵函数?Thanks@TanyaKiddle我仍然不理解这个问题,但在我的答案中添加了一个使用该函数的示例。
#create some vector
set.seed(42)
kv <- sample(-10:10,10)
#[1]  9  8 -5  4  0 -2  1 -9  5  2

#solve equation system
solve(bandmat(length(kv)),kv)
#[1]  0.0 -6.0 11.0 -6.5  1.0  2.5 -6.5 12.0 -8.5  2.0