From 51e9a135cc2ccca39126c81f812a91e812aeeea9 Mon Sep 17 00:00:00 2001 From: kevinyang1704 Date: Sun, 27 Sep 2015 10:26:46 -0400 Subject: [PATCH 1/2] Changes to be committed: modified: cachematrix.R --- cachematrix.R | 58 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..07f983d4d9b 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,61 @@ -## Put comments here that give an overall description of what your -## functions do +## makeCachematrix creates a special list of functions +## that calculate inverse of a matrix and caches it -## 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 + I <<- NULL + } + get <- function() x + setinv <- function(solve) I <<- solve + getinv <- function () I + list (set = set, get = get, + setinv = setinv, + getinv = getinv) } +makeVector <- function(x = numeric()) { + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setmean <- function(mean) m <<- mean + getmean <- function() m + list(set = set, get = get, + setmean = setmean, + getmean = getmean) +} -## Write a short comment describing this function +## This function computes the inverse of the special "matrix" +##returned by makeCacheMatrix above. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' +## Return a matrix that is the inverse of 'x' + I <-x$getinv() + if(!is.null(I)) { + message("getting cached data") + return(I) + } + data <-x$get() + I <-solve(data,...) + x$setinv(I) + I } + +cachemean <- function(x, ...) { + m <- x$getmean() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- mean(data, ...) + x$setmean(m) + m +} \ No newline at end of file From 2056a490b772aa9a2123cf2e2a1af348e45cee2d Mon Sep 17 00:00:00 2001 From: kevinyang1704 Date: Sun, 27 Sep 2015 14:44:19 -0400 Subject: [PATCH 2/2] final version --- cachematrix.R | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 07f983d4d9b..7db67600ed3 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -18,19 +18,6 @@ makeCacheMatrix <- function(x = matrix()) { getinv = getinv) } -makeVector <- function(x = numeric()) { - m <- NULL - set <- function(y) { - x <<- y - m <<- NULL - } - get <- function() x - setmean <- function(mean) m <<- mean - getmean <- function() m - list(set = set, get = get, - setmean = setmean, - getmean = getmean) -} ## This function computes the inverse of the special "matrix" ##returned by makeCacheMatrix above. @@ -47,15 +34,3 @@ cacheSolve <- function(x, ...) { x$setinv(I) I } - -cachemean <- function(x, ...) { - m <- x$getmean() - if(!is.null(m)) { - message("getting cached data") - return(m) - } - data <- x$get() - m <- mean(data, ...) - x$setmean(m) - m -} \ No newline at end of file