# Example of R code for simple matrix operations # # Create matrices A <- matrix(c(1,5, 2,1, 3,2), nrow=2, ncol=3) B <- matrix(c(2, 4, -2, 0, 3, 1), nrow=3, ncol=2) # Matrix addition (A plus transpose of B) A + t(B) # or so save to a matrix C C <- A + t(B) # Matrix multiplication AB <- A %*% B BA <- B %*% A # Note that the following does element by element multiplication AtB <- A * t(B) # Determinant of a matrix A <- matrix(c(4, 2, 1, 3, 3, 5, 3, 2, 2, 1, 2, 1, 1, 0, 1, 4), nrow=4, ncol=4) det <- det(A) # inverse of a matrix invA <- solve(A) # Check AinvA <- A %*% invA invAA <- invA %*% A