Guest User

Untitled

a guest
Mar 20th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. ## Put comments here that give an overall description of what your
  2. ## functions do
  3. # makeCacheMatrix function used to:
  4. #set the value of the matrix.
  5. #get the result value of the matrix.
  6. #set the value of inverse of the matrix.
  7. #get the value of inverse of the matrix.
  8.  
  9. ## Write a short comment describing this function
  10.  
  11. makeCacheMatrix <- function(x = matrix()) {
  12.  
  13. inv <- NULL
  14. set <- function(y) {
  15. x <<- y
  16. inv <<- NULL
  17.  
  18. }
  19. get <- function() x
  20. setinverse <- function(inverse) inv <<- inverse
  21. getinverse <- function() inv
  22. list(set=set, get=get, setinverse=setinverse, getinverse=getinverse)
  23. }
  24.  
  25.  
  26.  
  27. ## Write a short comment describing this function
  28. #The function below returns the inverse of the matrix.
  29. #First, it checks whether the inverse has been computed.
  30. #If it is, it shows the result and skips the computation.
  31. #If it is not, it will compute the inverse and set the value via setinverse function.
  32. #The function is assuming the matrix is always invertible.
  33. cacheSolve <- function(x, ...) {
  34. ## Return a matrix that is the inverse of 'x'
  35.  
  36. inv <- x$getinverse()
  37. if(!is.null(inv)) {
  38. message("getting cached data.")
  39. return(inv)
  40. }
  41. data <- x$get()
  42. inv <- solve(data)
  43. x$setinverse(inv)
  44. inv
  45. }
Add Comment
Please, Sign In to add comment