Saturday, 8 June 2013

R: Matrix Operations

Matrix manipulation in R are very useful in Linear Algebra. Below are list of common yet important functions in dealing operations with matrices:
  • Transpose - t;
  • Multiplication - %*%;
  • Determinant - det; and,
  • Inverse - solve, or ginv of MASS library
  • Eigenvalues and Eigenvectors - eigen
Consider these matrices, $\left[\begin{array}{ccc}3&4&5\\2&1&3\\6&5&4\end{array}\right]$ and  $\left[\begin{array}{ccc}6&7&5\\4&5&8\\7&6&6\end{array}\right]$. In R, these would be,

Transposing these, simply use t

Multiplying these, would be

For the determinant, we have

Taking the inverse of matrix1 is achieved by solve or ginv R functions. Note that ginv is in MASS package, and so we have

Finally, for eigenvalues and eigenvectors simply use eigen

The output above returns the $values, which is the eigenvalues, and $vectors, the eigenvectors.

No comments:

Post a Comment