From d07fcd71c9fcf5167db6828471aa2ebf465efb2e Mon Sep 17 00:00:00 2001 From: JoeData Date: Sun, 27 Apr 2014 15:55:58 -0500 Subject: [PATCH 1/2] added makeCacheMatrix & cacheSolve functions, and notes --- ProgrammingAssignment2 | 1 + 1 file changed, 1 insertion(+) create mode 160000 ProgrammingAssignment2 diff --git a/ProgrammingAssignment2 b/ProgrammingAssignment2 new file mode 160000 index 00000000000..7f657dd22ac --- /dev/null +++ b/ProgrammingAssignment2 @@ -0,0 +1 @@ +Subproject commit 7f657dd22ac20d22698c53b23f0057e1a12c09b7 From 1850b819aadfcdc1f179da5d41e3381a84fae294 Mon Sep 17 00:00:00 2001 From: JoeData Date: Sun, 27 Apr 2014 16:08:35 -0500 Subject: [PATCH 2/2] updated cachematrix.R --- ProgrammingAssignment2 | 1 - cachematrix.R | 45 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 7 deletions(-) delete mode 160000 ProgrammingAssignment2 diff --git a/ProgrammingAssignment2 b/ProgrammingAssignment2 deleted file mode 160000 index 7f657dd22ac..00000000000 --- a/ProgrammingAssignment2 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 7f657dd22ac20d22698c53b23f0057e1a12c09b7 diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..619b681d2a8 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,48 @@ -## Put comments here that give an overall description of what your -## functions do +## Coursera - R Programming - Programming Assignment 2 (peer assessment) +## +## Author - https://github.com/JoeData/ProgrammingAssignment2/ +## +## Functions - makeCacheMatrix, cacheSolve -## Write a short comment describing this function -makeCacheMatrix <- function(x = matrix()) { +## makeCacheMatrix accepts matrix, gets its inverse and stores inverse in cache +makeCacheMatrix <- function(x = matrix()) { + + ## setting variables + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + + ## inverting matrix and storing in cache + get <- function() x + setInverse <- function(solve) m <<- solve + getInverse <- function() m + list(set = set, get = get, + setInverse = setInverse, + getInverse = getInverse) } -## Write a short comment describing this function +## cacheSolve accepts matrix, checks if its inverse is cached +## if cached, returns the cached inverted matrix +## if not cached, inverts the matrix and returns cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + + m <- x$getInverse() + + ## checking if inverted matrix is cached and if so, returns + if(!is.null(m)) { + message("getting cached data") + return(m) + } + + ## if not in cache, inverts and returns + data <- x$get() + m <- solve(data, ...) + x$setInverse(m) + m }