# R Language Subsetting Elementwise Matrix Operations

## Example

Let A and B be two matrices of same dimension. The operators `+`,`-`,`/`,`*`,`^` when used with matrices of same dimension perform the required operations on the corresponding elements of the matrices and return a new matrix of the same dimension. These operations are usually referred to as element-wise operations.

OperatorA op BMeaning
+A + BAddition of corresponding elements of A and B
-A - BSubtracts the elements of B from the corresponding elements of A
/A / BDivides the elements of A by the corresponding elements of B
*A * BMultiplies the elements of A by the corresponding elements of B
^A^(-1)For example, gives a matrix whose elements are reciprocals of A

For "true" matrix multiplication, as seen in Linear Algebra, use `%*%`. For example, multiplication of A with B is: `A %*% B`. The dimensional requirements are that the `ncol()` of `A` be the same as `nrow()` of `B`

## Some Functions used with Matrices

FunctionExamplePurpose
nrow()nrow(A)determines the number of rows of A
ncol()ncol(A)determines the number of columns of A
rownames()rownames(A)prints out the row names of the matrix A
colnames()colnames(A)prints out the column names of the matrix A
rowMeans()rowMeans(A)computes means of each row of the matrix A
colMeans()colMeans(A)computes means of each column of the matrix A
upper.tri()upper.tri(A)returns a vector whose elements are the upper
triangular matrix of square matrix A
lower.tri()lower.tri(A)returns a vector whose elements are the lower
triangular matrix of square matrix A
det()det(A)results in the determinant of the matrix A
solve()solve(A)results in the inverse of the non-singular matrix A
diag()diag(A)returns a diagonal matrix whose off-diagnal elemts are zeros and
diagonals are the same as that of the square matrix A
t()t(A)returns the the transpose of the matrix A
eigen()eigen(A)retuens the eigenvalues and eigenvectors of the matrix A
is.matrix()is.matrix(A)returns TRUE or FALSE depending on whether A is a matrix or not.
as.matrix()as.matrix(x)creates a matrix out of the vector x