diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..67c4e1dd387 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,54 @@ -## Put comments here that give an overall description of what your -## functions do +## This function returns the inverse matrix of a nonsingular matrix specified +## by users. -## Write a short comment describing this function +## makeCacheMatrix returns a function list that would be used in cacheSolve makeCacheMatrix <- function(x = matrix()) { - + + ## initialize an object "m", which will be used to store inverse matrix + m <-NULL + + ## assign the input into "x", and also initialize the "m". + set <-function(y){ + x <- y + m <- NULL + } + + get <- function() x ## retrives the matrix stored in "x" + + setInv <- function(inv) m<<-inv ## store the input into "m" + + getInv <- function() m ## return the value of "m" + + ## combine the four functions into a list variable as an output + list(set = set, get = get, + setInv = setInv, + getInv = getInv) + } -## Write a short comment describing this function +## cacheSolve function is for retriving the cache if it exist, or create one +## and store the inverse matrix in it if cache does not exist. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + + m<- x$getInv() + + ## if "m" has a value in it, return this value (the inverse matrix) + ## and exit the function, otherwise jump to the next command. + if(!is.null(m)) { + message("getting cached inverse matrix") + return(m) + } + + ## if "m" is null, calculate the inverse of original matrix using + ## solve function. + data <- x$get() + m <- solve(data,...) + + ## store the result in "m", and return the value of "m", + ## which is the inverse of "x" + x$setInv(m) + m }