對於大型sparse matrices,使用Matrix package:
library(Matrix)
## Construct a memory-efficient representation of a large, sparse, diagonal matrix
dmat <- Diagonal(x=1:14000)
## Finding the inverse is fast and painless
system.time(dmat.inv <- solve(dmat))
# user system elapsed
# 0 0 0
## Check that it worked
dmat.inv[c(1:3, 13998:14000), c(1:3, 13998:14000)]
# 6 x 6 diagonal matrix of class "ddiMatrix"
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 1 . . . . .
# [2,] . 0.5 . . . .
# [3,] . . 0.3333333 . . .
# [4,] . . . 7.143878e-05 . .
# [5,] . . . . 7.143367e-05 .
# [6,] . . . . . 7.142857e-05
還要注意的是對角線'M'那簡直是'1/M'。 – Julius
如果你的矩陣不是*稀疏的,這將是不可能的(或者至少非常非常非常困難)。根據你想要做什麼,通常有計算捷徑來解決實際上不需要反轉矩陣的問題(例如求解線性系統,計算特徵值等) –