Skip to content

Instantly share code, notes, and snippets.

@smc77
Created October 20, 2011 01:32
Show Gist options
  • Save smc77/1300192 to your computer and use it in GitHub Desktop.
Save smc77/1300192 to your computer and use it in GitHub Desktop.
Quick linear algebra demo
# Matrix addition
matrix(c(1, 2, 3, 0, 5, 1), ncol=2) + matrix(c(4, 2, 0, 0.5, 5, 1), ncol=2)
# Matrix multiplication
3 * matrix(c(1, 2, 3, 0, 5, 1), ncol=2)
# Matrix-Vector Multiplication
matrix(c(1, 4, 2, 3, 0, 1), ncol=2) %*% c(1, 5)
# Matrix-Mector Multiplication
matrix(c(1, 2, 3, 0, 5, 1), ncol=2) %*% t(matrix(c(4, 2, 0, 0.5, 5, 1), ncol=2))
# Transpose
t(matrix(c(4, 2, 0, 0.5, 5, 1), ncol=2))
# Inverse
solve(matrix(c(4, 2, 0, 0.5, 5, 1), ncol=2))
# Diagonal matrix * Inverse = Identity
diag(c(1, 2, 3)) * solve(diag(c(1, 2, 3)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment