diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..7db67600ed3 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,36 @@ -## Put comments here that give an overall description of what your -## functions do +## makeCachematrix creates a special list of functions +## that calculate inverse of a matrix and caches it -## Write a short comment describing this function +## This function creates a special "matrix" object that can +##cache its inverse. makeCacheMatrix <- function(x = matrix()) { - + I <- NULL + set <- function(y) { + x <<- y + I <<- NULL + } + get <- function() x + setinv <- function(solve) I <<- solve + getinv <- function () I + list (set = set, get = get, + setinv = setinv, + getinv = getinv) } -## Write a short comment describing this function +## This function computes the inverse of the special "matrix" +##returned by makeCacheMatrix above. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' +## Return a matrix that is the inverse of 'x' + I <-x$getinv() + if(!is.null(I)) { + message("getting cached data") + return(I) + } + data <-x$get() + I <-solve(data,...) + x$setinv(I) + I }