options(repos=c(CRAN="http://mirrors.ustc.edu.cn/CRAN/")) ##Matrix algebra in R ##------------------------------------------------------------------------------------ #1. Basic matrix manipulation A<-matrix(c(2,3,4,5),2,2) B<-matrix(c(1,2,3,4),2,2) A*B #Element-wise multiplication cbind(A,B) #Combine matrices(vectors) horizontally. Returns a matrix. rbind(A,B) #Combine matrices(vectors) vertically. Returns a matrix. rowMeans(A) #Returns vector of row means. rowSums(A) #Returns vector of row sums. colMeans(A) #Returns vector of column means. colSums(A) #Returns vector of coumn means. #2. matrix multiplication A%*%B #3. outer product, AB' A%o%B #4. A'B and A'A respectively crossprod(A,B) corssprod(A) #5. transpose t(A) #6. diag(x) Creates diagonal matrix with elements of x in the principal diagonal # diag(A) Returns a vector containing the elements of the principal diagonal # diag(k) If k is a scalar, this creates a k x k identity matrix. Go figure. diag(c(2,3,4)) diag(A) diag(4) #7. solve(A, b) Returns vector x in the equation b = Ax (i.e., A-1b) #solve(A) Inverse of A where A is a square matrix. b<-c(1,2) solve(A,b) solve(A) #8. ginv(A) Moore-Penrose Generalized Inverse of A. #ginv(A) requires loading the MASS package. library(MASS) ginv(A) #9. y<-eigen(A) y\$val are the eigenvalues of A #y\$vec are the eigenvectors of A eigen(A) #10. y<-svd(A) Single value decomposition of A, A=UDV' #y\$d = vector containing the singular values of A #y\$u = matrix with columns contain the left singular vectors of A #y\$v = matrix with columns contain the right singular vectors of A svd(A) #11. R <- chol(A) Choleski factorization of A. Returns the upper triangular factor, such that R'R = A. A<-matrix(c(2,3,3,5),2,2) chol(A) #12. y <- qr(A) QR decomposition of A. #y\$qr has an upper triangle that contains the decomposition and a lower triangle that contains information on the Q decomposition. #y\$rank is the rank of A. #y\$qraux a vector which contains additional information on Q. #y\$pivot contains information on the pivoting strategy used. qr(A) #----------------------------------------------------------------------------------------------- #The Matrix package contains functions that extend R to support highly dense or sparse matrices. #It provides efficient access to BLAS (Basic Linear Algebra Subroutines), Lapack (dense matrix), #TAUCS (sparse matrix) and UMFPACK (sparse matrix) routines. library("Matrix") set.seed(7) m <- matrix(0, 5, 5) m[sample(length(m), size = 14)] <- rep(1:9, length=14) mm <- as(m, "CsparseMatrix") tril(mm) # lower triangle tril(mm, -1) # strict lower triangle triu(mm, 1) # strict upper triangle band(mm, -1, 2) # general band data(KNex) names(KNex) mtm <- with(KNex, crossprod(mm)) dq <- function(ch) paste('"',ch,'"', sep="") ## dQuote() gives bad plots image(mtm, main=paste("crossprod(mm): Sparse", dq(class(mtm)))) C1 <- Cholesky(mtm) # uses show() cm1 <- as(C1, "sparseMatrix") image(cm1, main= paste("as(Cholesky(crossprod(mm)),\"sparseMatrix\"):", dq(class(cm1)))) ####################### #Matlab Emulation #The matlab package contains wrapper functions and variables used to replicate MATLAB function calls as best possible. #This can help porting MATLAB applications and code to R.