R言語で多変量解析3(行列演算)
行列演算
- 行列の掛け算:%*%
-
abline(a, b):y切片aと傾きbを持つ直線を引く
-
curve(関数, 最小値, 最大値):ある関数の曲線を描く関数
> #行列,2×2で初期値0.0
> M <- matrix(0.0, 2, 2)
> M
[,1] [,2]
[1,] 0 0
[2,] 0 0
> #1行目と2行目に値を設定(45度の回転行列)
> M[1,] <- c( cos(pi / 4), -sin(pi / 4) )
> M[2,] <- c( sin(pi / 4), cos(pi / 4) )
> M
[,1] [,2]
[1,] 0.7071068 -0.7071068
[2,] 0.7071068 0.7071068
> u <- matrix( c(1,0), 2, 1 )
> u
[,1]
[1,] 1
[2,] 0
> v <- M %*% u #行列の掛け算 %*%> v #u(1,0)を45度で回転
[,1]
[1,] 0.7071068
[2,] 0.7071068
> x <- cbind( u[1,], v[1,] )
> y <- cbind( u[2,], v[2,] )
> x
[,1] [,2]
[1,] 1 0.7071068
> y
[,1] [,2]
[1,] 0 0.7071068
> plot( x, y, xlim = c(0,1), ylim = c(0,1), pch = 20, cex = 5, col = "red" )
> abline( 0, 0 ) #y=0の直線
> abline( 0, 1 ) #y=xの直線
> curve( sqrt( 1 - x*x ), 1/sqrt(2), 1, xlim = c(0,1), add = TRUE ) #単位円の関数√1-x^2>