From c5a55fde2958e0a15decb2b90ee04be6fc33c633 Mon Sep 17 00:00:00 2001 From: Gentle Yang Date: Fri, 19 Dec 2014 18:19:47 +0800 Subject: [PATCH] finish two functions --- cachematrix.R | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..e7205f8d41c 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,33 @@ -## Put comments here that give an overall description of what your -## functions do +## solve with cache -## Write a short comment describing this function +## This function creates a special "matrix" object that can cache its inverse. makeCacheMatrix <- function(x = matrix()) { - + ivs <- NULL + set <- function(y) { + x <<- y + ivs <<- NULL + } + get <- function() x + setinverse <- function(inverse) ivs <<- inverse + getinverse <- function() ivs + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } -## Write a short comment describing this function +## cache martix inverse function cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + ivs <- x$getinverse() + if(!is.null(ivs)) { + message("getting cached matrix inverse") + return(ivs) + } + data <- x$get() + ivs <- solve(data, ...) + x$setinverse(ivs) + ivs }