関数として用意されているものいろいろ Last modified: Feb 17, 2009
行列式 det 直前のページへ戻る
> (a <- matrix(c(1, 2, 4, 9), nrow=2))
[,1] [,2]
[1,] 1 4
[2,] 2 9
> det(a)
[1] 1
> (b <- matrix(c(2,5,4,3, 5,7,6,3, 5,8,7,9, 1,2,5,3), byrow=TRUE, ncol=4))
[,1] [,2] [,3] [,4]
[1,] 2 5 4 3
[2,] 5 7 6 3
[3,] 5 8 7 9
[4,] 1 2 5 3
> det(b)
[1] 186
> det(matrix(rnorm(12), 3))
以下にエラー determinant.matrix(x, logarithm = TRUE, ...) :
'x' は正方行列でなければなりません
逆行列 solve 直前のページへ戻る
> (a <- matrix(c(1, 2, 4, 9), nrow=2))
[,1] [,2]
[1,] 1 4
[2,] 2 9
> solve(a)
[,1] [,2]
[1,] 9 -4
[2,] -2 1
> (b <- matrix(c(2,5,4,3, 5,7,6,3, 5,8,7,9, 1,2,5,3), byrow=TRUE, ncol=4))
[,1] [,2] [,3] [,4]
[1,] 2 5 4 3
[2,] 5 7 6 3
[3,] 5 8 7 9
[4,] 1 2 5 3
> solve(b)
[,1] [,2] [,3] [,4]
[1,] -0.67741935 0.35483871 0.12903226 -0.06451613
[2,] 0.54838710 -0.09677419 -0.08064516 -0.20967742
[3,] -0.03225806 0.06451613 -0.11290323 0.30645161
[4,] -0.08602151 -0.16129032 0.19892473 -0.01612903
> solve(matrix(rnorm(12), 3))
以下にエラー solve.default(matrix(rnorm(12), 3)) :
逆行列は正方行列だけにしか定義できません
一般化逆行列 MASS:::ginv 直前のページへ戻る
> library(MASS)
> (x <- matrix(c(1,3,2, 5,3,2), byrow=TRUE, ncol=3))
[,1] [,2] [,3]
[1,] 1 3 2
[2,] 5 3 2
> ginv(x)
[,1] [,2]
[1,] -0.2500000 0.25000000
[2,] 0.2884615 -0.05769231
[3,] 0.1923077 -0.03846154
連立方程式 solve 直前のページへ戻る
> (a <- c(3,2,1,5))
[1] 3 2 1 5
> (b <- matrix(c(2,5,4,3, 5,7,6,3, 5,8,7,9, 1,2,5,3), byrow=TRUE, ncol=4))
[,1] [,2] [,3] [,4]
[1,] 2 5 4 3
[2,] 5 7 6 3
[3,] 5 8 7 9
[4,] 1 2 5 3
> solve(b, a)
[1] -1.5161290 0.3225806 1.4516129 -0.4623656
> (c <- matrix(c(3,2,1,5, 4,3,2,7), nrow=4))
[,1] [,2]
[1,] 3 4
[2,] 2 3
[3,] 1 2
[4,] 5 7
> solve(b, c)
[,1] [,2]
[1,] -1.5161290 -1.8387097
[2,] 0.3225806 0.2741935
[3,] 1.4516129 1.9838710
[4,] -0.4623656 -0.5430108
固有値・固有ベクトル eigen 直前のページへ戻る
> (x <- matrix(c(1,2,3, 2,1,5, 3,5,1), 3))
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 2 1 5
[3,] 3 5 1
> eigen(x)
$values
[1] 7.8390863 -0.7106916 -4.1283947
$vectors
[,1] [,2] [,3]
[1,] -0.4612978 0.8691432 -0.1783099
[2,] -0.6075927 -0.4559069 -0.6503692
[3,] -0.6465566 -0.1916741 0.7383939
> (y <- matrix(c(1,3,2, 7,5,3, 2,4,8), 3))
[,1] [,2] [,3]
[1,] 1 7 2
[2,] 3 5 4
[3,] 2 3 8
> eigen(y)
$values
[1] 12.087767 3.789629 -1.877395
$vectors
[,1] [,2] [,3]
[1,] -0.4828242 -0.6512015 -0.9297536
[2,] -0.5764175 -0.4368246 0.3595983
[3,] -0.6592600 0.6205811 0.0790403
特異値分解 svd 直前のページへ戻る
> (x <- matrix(c(1,2,3, 2,1,5, 3,5,1), 3))
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 2 1 5
[3,] 3 5 1
> svd(x)
$d
[1] 7.8390863 4.1283947 0.7106916
$u
[,1] [,2] [,3]
[1,] -0.4612978 -0.1783099 -0.8691432
[2,] -0.6075927 -0.6503692 0.4559069
[3,] -0.6465566 0.7383939 0.1916741
$v
[,1] [,2] [,3]
[1,] -0.4612978 0.1783099 0.8691432
[2,] -0.6075927 0.6503692 -0.4559069
[3,] -0.6465566 -0.7383939 -0.1916741
直前のページへ戻る E-mail to Shigenobu AOKI