From 0f86cb1404f5bfb3b2f3c43ac1b6f0e363d757e7 Mon Sep 17 00:00:00 2001 From: Ali Mahmoud Date: Sat, 28 May 2016 05:22:21 +0200 Subject: [PATCH] makeCacheMatrix. --- cachematrix.R | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..15294b35956 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,40 @@ -## Put comments here that give an overall description of what your -## functions do +## These functions compute the inverse of the special "matrix" returned by +## makeCacheMatrix. If the inverse has already been calculated +## (and the matrix has not changed), then cacheSolve should retrieve the +## inverse from the cache. -## Write a short comment describing this function +## This function creates a special "matrix" object that can cache its inverse. makeCacheMatrix <- function(x = matrix()) { - + s <- NULL; + set <- function(y) { + x <<- y; + s <- NULL; + } + + get <- function() x; + + setInverse <- function(inverse) s <- inverse; + + getInverse <- function() s; + + list(set=set, get=get, setInverse=setInverse, getInverse=getInverse); } -## Write a short comment describing this function +## This function computes the inverse of the special "matrix" returned by +## makeCacheMatrix above. If the inverse has already been calculated +## (and the matrix has not changed), then cacheSolve should retrieve the +## inverse from the cache. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + s <- x$getInverse() + if(!is.null(s)) { + message("getting cached data"); + } else { + data <- x$get(); + s <- solve(data, ...); + x$setInverse(s); + } + return(s); }