From 1a3c7275a45c57fa540283ef87bf9bf0af663b96 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 16 Apr 2014 15:42:47 +0800 Subject: [PATCH 1/2] complete the functions --- cachematrix.R | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..6007c551cf2 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,36 @@ -## Put comments here that give an overall description of what your -## functions do +# Matrix inversion is usually a costly computation and their may be some benefit +# to caching the inverse of a matrix rather than compute it repeatedly -## Write a short comment describing this function +# This function creates a special "matrix" object that can cache its inverse. makeCacheMatrix <- function(x = matrix()) { - + i <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinverse <- function(inverse) i <<- inverse + getinverse <- function() i + 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 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' + i <- x$getinverse() + if (!is.null(i)) { + message("gettting cached data") + return(i) + } + + data <- x$get() + i <- solve(data, ...) + x$setinverse(i) + i } From a5320b70038d3356b148044459bda1357d1d98b8 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 16 Apr 2014 15:48:53 +0800 Subject: [PATCH 2/2] fix comment --- cachematrix.R | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 6007c551cf2..cd22d6376ce 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,9 +1,8 @@ # Matrix inversion is usually a costly computation and their may be some benefit # to caching the inverse of a matrix rather than compute it repeatedly -# This function creates a special "matrix" object that can cache its inverse. - makeCacheMatrix <- function(x = matrix()) { + # This function creates a special "matrix" object that can cache its inverse. i <- NULL set <- function(y) { x <<- y @@ -15,14 +14,11 @@ makeCacheMatrix <- function(x = matrix()) { list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) } - -# 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' + # 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. i <- x$getinverse() if (!is.null(i)) { message("gettting cached data")