diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..6aa1da1db2b 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,36 @@ ## Put comments here that give an overall description of what your ## functions do -## Write a short comment describing this function - +## ## This function will creat a special "matrix", which is actually a list of four founction. makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setsolve <- function(solve) m <<- solve ##assign the inverse to m + getsolve <- function() m + list(set = set, get = get, + setsolve = setsolve, + getsolve = getsolve) } -## Write a short comment describing this function +## This function will return the inverse of x. if there are cached one, it will skip the calculation cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + m <- x$getsolve() + if(!is.null(m)) { + message("getting cached data") ##skip the calculation when there are cached one + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setsolve(m) + m + } + +