From 47d7a3256cc33b84d982c14af0a42fe528e3dc29 Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Sat, 19 Dec 2015 21:08:07 +0000 Subject: [PATCH] Version 1.0 --- cachematrix.R | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..d30937130ac 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 +## These functions allow for caching the inverse of the matrix +## to prevent repeated calculation of the value +## makeCacheMatrix creates the special matrix variable +## using matrix 'x' as the argument with ability to keep track +## whether its Inverse value has been already calculated makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setInverse <- function(inverse) m <<- inverse + getInverse <- function() m + list(set = set, get = get, + setInverse = setInverse, + getInverse = getInverse) } - -## Write a short comment describing this function - +## cacheSolve function returns inverse of the special matrix x +## - from the cache if it has been previously calculated, or +## - freshly calculated, and stores this value in the cache cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + m <- x$getInverse() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- solve(data) + x$setInverse(m) + m + }