Section 18 Matrix Operation: Mathematical

18.1 Matrix operation

Function Explanation Example..x.is.square.matrix.
t Transpose t(x)
diag Diagonal diag(x)
%*% Inner (dot) product of two vectors t(x) and y, and conventional matrix multiplication x %*% y
%o% Outer product of two vectors x and t(y) x %o% y
crossprod Cross products t(x) and y crossprod(x,y)
tcrossprod Cross products x and t(y) tcrossprod(x,y)
det Determinant det(x)
solve Inverse solve(x)
eigen Eigenvalues and eigenvectors eigen(x)
svd Singular value decomposition svd(x)
qr QR decomposition qr(x)
chol Choleski decomposition chol(x)

18.2 Example 1

x <- matrix(c(7, 2, 5, 9), nrow=2, ncol=2)
x+5
x-5
x*5
x/2
x^2
-x
x > 3

18.3 Example 2

# Elementwise operation
x <- matrix(c(7, 2, 5, 9), nrow=2, ncol=2)
y <- matrix(c(3, 9, 5, 7), nrow=2, ncol=2)
t(x)
diag(x)
solve(x)
det(x)
t(x) %*% y