From 0c2f838c1f37203e22b47a7ce8f982397ef79325 Mon Sep 17 00:00:00 2001 From: SirAlf Date: Sun, 13 Apr 2014 23:16:57 +0800 Subject: [PATCH 1/3] my commit to peer assessment assignment --- cachematrix.R | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..a6244985609 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,45 @@ -## Put comments here that give an overall description of what your -## functions do +## This function caches the inverse of a matrix +## -## Write a short comment describing this function +## The makeCacheMatrix creates a special "matrix" object that can cache +## its Inverse makeCacheMatrix <- function(x = matrix()) { + inv <- NULL + set <- function(y) { + x <<- y + inv <<- NULL + } + + get <- function() x + setinv <- function(solve) inv <<- solve + getinv <- function() inv + + list(set = set, get = get, + setinv = setinv, + getinv = getinv) } +## The cacheSolve function computes the inverse of the +## special "matrix" returned by makeCacheMatrix if it +## has not been cached. -## Write a short comment describing this function +## If the inverse has been cached, it will return the cached value cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + + inv <- x$getinv() + + if(!is.null(inv)) { + message("getting cached data") + return(inv) + } + + data <- x$get() + inv <- solve(data, ...) + x$setinv(inv) + inv + } From e5115478224d68b8df8019ab7223fec49b21971c Mon Sep 17 00:00:00 2001 From: SirAlf Date: Fri, 16 Jan 2015 10:16:44 +0800 Subject: [PATCH 2/3] recommit updated ProgrAssgnmt2 --- cachematrix.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index a6244985609..af9ef620cf7 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,4 +1,4 @@ -## This function caches the inverse of a matrix +## This function caches the inverse of a matrix ## ## The makeCacheMatrix creates a special "matrix" object that can cache From 57b2efa7705973f53cbefc7e43a587ec5969ea6d Mon Sep 17 00:00:00 2001 From: SirAlf Date: Mon, 2 Oct 2017 17:21:36 +0800 Subject: [PATCH 3/3] RProgPA2Date2017Oct I retook the course after a couple of years, but this time I am aiming for the certificate --- .Rhistory | 0 cachematrix.R | 3 ++- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .Rhistory diff --git a/.Rhistory b/.Rhistory new file mode 100644 index 00000000000..e69de29bb2d diff --git a/cachematrix.R b/cachematrix.R index af9ef620cf7..bcebb410a0a 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -15,7 +15,8 @@ makeCacheMatrix <- function(x = matrix()) { setinv <- function(solve) inv <<- solve getinv <- function() inv - list(set = set, get = get, + list(set = set, + get = get, setinv = setinv, getinv = getinv)