From f07faaeada631f1490b0c8c13ba5ee0923c52571 Mon Sep 17 00:00:00 2001 From: DF Date: Sun, 22 Feb 2015 09:34:48 +1100 Subject: [PATCH 1/2] Changes in makeCacheMatrix and cacheSolve Added comments, and creation of makeCacheMatrix body as well as cacheSolve --- cachematrix.R | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..3f536b7da8d 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,38 @@ ## Put comments here that give an overall description of what your ## functions do +## For costly computation sometimes there's benefit caching the functions. +## The purpose of the functions below is to cache the inverse of a matrix rathen than compute it repeatedly. ## Write a short comment describing this function - +## Creates a special "matrix" object that can cache its inverse 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 - +##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 the cachesolve should retrieve the inverse from the cache cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of '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 } From 5f067d7dfea30e5168948293c73707bf437a6f84 Mon Sep 17 00:00:00 2001 From: DF Date: Sun, 22 Feb 2015 09:38:14 +1100 Subject: [PATCH 2/2] spaces and typo Fixing spaces to keep consistency and typo --- cachematrix.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 3f536b7da8d..deaf88cced3 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,7 +1,7 @@ ## Put comments here that give an overall description of what your ## functions do ## For costly computation sometimes there's benefit caching the functions. -## The purpose of the functions below is to cache the inverse of a matrix rathen than compute it repeatedly. +## The purpose of the functions below is to cache the inverse of a matrix rather than compute it repeatedly. ## Write a short comment describing this function ## Creates a special "matrix" object that can cache its inverse @@ -21,9 +21,9 @@ makeCacheMatrix <- function(x = matrix()) { ## Write a short comment describing 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 the cachesolve should retrieve the inverse from the cache +## 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 the cachesolve should retrieve the inverse from the cache cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' m <- x$getinverse()