## create a matrix A <- matrix(c(3, 1, -1, 5, 2, 4,5,7,3,6,5,3), nrow=2, ncol=6) ## print the matrix A ## get the (2,6)th element of the matrix a.2.6 <- A[2,6] a.2.6 ## get row = 1 and colums 1,4,6 b <- A[1,c(1,4,6)] b ## now subset the matrix A again and call it A A1 <- A[, 1:3] A1 ## or subtract of the elements you dont want A2 <- A[, -c(4:6)] A2 ## subtract A1 from A2 A3 <- A1 - A2 A3 ## set A1 or A2 to A A <- A1 ## multiply A by a scalar equal to 5 out <- 5*A out ## take the transpose of A trans.A <- t(A) trans.A ## create two more matrices B <- matrix(c(-2,7,9), nrow=3, ncol=1) C <- matrix(c(2,0,1,-1), nrow=2, ncol=2) ## matrix multiplication AB <- A%*%B AB CA <- C%*%A CA ## calculate the determinent of C det.C <- det(C) det.C ## the solve(A,b) funtion is used to solve Ax=b ## A is the matrix of coefficients and b can be a vector or a matrix ## if b is the identity matrix of the same size as A, then the solve command ## gives the inverse of the matrix inv.C <- solve(C) inv.C ## the eigenvalue example A <- matrix(c(1,1,0,3), nrow=2, ncol=2) eig.A <- eigen(A) eig.A ## get the eigenvalues eig.A$values ## get the eigenvectors eig.A$vectors ## check the length of the second eigenvector e2 <- eig.A$vectors[,2] l.e2 <- sqrt(t(e2)%*%e2) l.e2 ## now make a function for to calculate the trace of a matrix trace <- function(X){ out <- sum(diag(X)) return(out) } trace(A)