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.

Operator | A op B | Meaning |
---|---|---|

+ | A + B | Addition of corresponding elements of A and B |

- | A - B | Subtracts the elements of B from the corresponding elements of A |

/ | A / B | Divides the elements of A by the corresponding elements of B |

* | A * B | Multiplies 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`

Function | Example | Purpose |
---|---|---|

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 |

This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0

This website is not affiliated with Stack Overflow