From acb93c1e8acab7c7e1c24edcb98cfeba4c7be5c9 Mon Sep 17 00:00:00 2001 From: Tomas Sirny Date: Sun, 25 Oct 2015 15:29:14 +0100 Subject: [PATCH] Working CacheMatrix --- cachematrix.R | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..2ceebb8e48b 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,33 @@ -## Put comments here that give an overall description of what your -## functions do +## Functions for computing inverse matrix for given matrix, caching +## the result for future use within the program -## Write a short comment describing this function +## Special type of matrix, which can save also its inverse matrix makeCacheMatrix <- function(x = matrix()) { - + im <- NULL + set <- function(y) { + x <<- y + im <<- NULL + } + get <- function() x + setinverse <- function(inverse) im <<- inverse + getinverse <- function() im + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } -## Write a short comment describing this function +## Function for computing inverse matrix, caching the result within CacheMatrix cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + im <- x$getinverse() + if(!is.null(im)) { + message("getting cached data") + return(im) + } + data <- x$get() + im <- solve(data, ...) + x$setinverse(im) + im }