如何更准确地计算逆矩阵?

时间:2014-08-01 21:56:52

标签: r max matrix-inverse

我跑了一些代码:

size <- 5
G < -matrix(c(0,1,0,0,1,
              1,0,1,1,0,
              0,1,0,1,1,
              0,1,1,0,1,
              1,0,1,1,0), ncol=size)

# Max <- rep(0,4)
delta <- 0.299
results <- rep(0,16)
results <- matrix(results, ncol=4)
p <- 1
record <- NULL
for (i in 1:(size-1))
 {
  for(j in (i+1):size )
   {
     GAA <- matrix(c(G[i,i], G[j,i], G[i,j], G[j,j]), ncol=2)
     GAB <- rbind(G[i, c(-i,-j)], G[j, c(-i,-j)])
     GBA <- t(GAB)
     GBB <- G[c(-i,-j),c(-i,-j)]
     I <- diag(dim(GBB)[1])
     U <- solve(I-delta*GBB)
     T <- GAA+delta*GAB%*%U%*%GBA
     TD <- diag(diag(T))
     IT <- diag(dim(T)[1])
     S11 <- solve(IT-delta*(T+TD))
     S12 <- delta*S11%*%GAB%*%U
     S21 <- t(S12)
     S22 <- U + delta^2 * U %*% GBA %*% S11 %*% GAB %*% U
     Total <- sum(rbind(cbind(S11,S12), cbind(S21,S22)))
     record <- rbind(record, c(Total,i,j))
   }
 }
 record

来自record,我可以看到:

           [,1] [,2] [,3]
 [1,]  124.0335    1    2
 [2,] -210.0012    1    3
 [3,] -210.0012    1    4
 [4,]  124.0335    1    5
 [5,]  247.8299    2    3
 [6,]  247.8299    2    4
 [7,] -273.4414    2    5
 [8,]  163.5456    3    4
 [9,]  247.8299    3    5
[10,]  247.8299    4    5

显示有四行给出最高的和(行[5],[6],[9],[10])。  但是,在我运行以下代码后:

Max <- max(record[,1])
for(l in 1:(size*(size-1)/2))
 {
   if (record[l,1]==Max)
   {
     results[p,] <- c(record[l,],delta)
     p <- p+1}
 }
results:
         [,1] [,2] [,3]  [,4]
[1,] 247.8299    2    3 0.299
[2,] 247.8299    2    4 0.299
[3,]   0.0000    0    0 0.000
[4,]   0.0000    0    0 0.000

我只捕捉行[5]和[6];我在record上遗漏了行[9]和[10]。 问题在哪里?

0 个答案:

没有答案