diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..99152820d11 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 - +## The first function, makeCacheMatrix creates a special "matrix", +## which is really a list containing a function to: +## set the value of the matrix +## get the value of the matrix +## set the value of the inverse matrix +## get the value of the inverse matrix makeCacheMatrix <- function(x = matrix()) { - + im <- NULL + set <- function(y) { + x <<- y + inverseMatrix <<- NULL + } + get <- function() x + setinverse <- function(inverseMatrix) im <<- inverseMatrix + getinverse <- function() im + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } -## Write a short comment describing this function +## The second function calulates inverse matrix, that is stored +## in object function above, or returns cached inverse of the matrix cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + im <- x$getinverse() + if(!is.null(im)) { + message("getting cached matrix") + return(im) + } + data <- x$get() + im <- solve(data) + x$setinverse(im) + im }