diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..cb8efa870dd 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,30 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## This funciton uses solve() function. makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinverse <- function(solve) m <<- solve + getinverse <- function() m + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } - - -## Write a short comment describing this function +## cacheSolve function check first if there matrix inverse is already calculated, else calculates using solve() function described above. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + m <- x$getinverse() + if(!is.null(m)) { + message("display cached data") + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setinverse(m) + m + ## This method return a matrix that is the inverse of 'x' } +