Skip to content

Instantly share code, notes, and snippets.

@dirmeier
Last active April 23, 2018 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dirmeier/9dcdc3a79022628a606a6cec0976bb61 to your computer and use it in GitHub Desktop.
Save dirmeier/9dcdc3a79022628a606a6cec0976bb61 to your computer and use it in GitHub Desktop.
Matrix inverses
set.seed(23)
# Create a positive-definite matrix
# a Wishart random variable + the identity matrix should fullfil this. Any positive-definite matrix is invertible.
m <- rWishart(.1, 5, diag(5))[,,1] + diag(5)
# set the bottom and right to 1
# by this the top left is positive definite, but the complete matrix is singular, i.e. not invertible
m[4:5, ] <- 1
m[, 4:5] <- 1
# works
solve(m[1:3, 1:3])
# fails, due to rank deficiency
solve(m)[1:3, 1:3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment