Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. @@ -1,15 +1,41 @@
  2. ## Put comments here that give an overall description of what your
  3. ## functions do
  4.  
  5. ## Write a short comment describing this function
  6. ## Aim in this experiment is to write a pair of functions,
  7. ## "makeCacheMatrix" and "cacheSolve" that cache the inverse of a matrix
  8. ## makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse.
  9. ## cacheSolve: This function computes the inverse of the special "matrix" returned by makeCacheMatrix above.
  10. ## makeCacheMatrix is a function which creates a special "matrix" object that can
  11. ## cache its inverse for the input (which is an invertible square matrix)
  12.  
  13. makeCacheMatrix <- function(x = matrix()) {
  14.  
  15. inv <- NULL
  16. set <- function(y) {
  17. x <<- y
  18. inv <<- NULL
  19. }
  20. get <- function() x
  21. setinv <- function(inverse) inv <<- inverse
  22. getinv <- function() inv
  23. list(set = set, get = get, setinv = setinv, getinv = getinv)
  24. }
  25.  
  26.  
  27. ## Write a short comment describing this function
  28. ## cacheSolve is a function which computes the inverse of the special "matrix"
  29. ## returned by makeCacheMatrix above. If the inverse has already been calculated
  30. ## (and the matrix has not changed), then the cachesolve should retrieve the
  31. ## inverse from the cache
  32.  
  33. cacheSolve <- function(x, ...) {
  34. ## Return a matrix that is the inverse of 'x'
  35. inv <- x$getinv()
  36. if(!is.null(inv)) {
  37. message("getting cached result")
  38. return(inv)
  39. }
  40. data <- x$get()
  41. inv <- solve(data, ...)
  42. x$setinv(inv)
  43. inv
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement