diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..807ea251739 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.Rproj.user +.Rhistory +.RData diff --git a/ProgrammingAssignment2.Rproj b/ProgrammingAssignment2.Rproj new file mode 100644 index 00000000000..066341ea144 --- /dev/null +++ b/ProgrammingAssignment2.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 4 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..2aa25d6637f 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,46 @@ -## Put comments here that give an overall description of what your -## functions do +## These functions provide a way to create an inverted matrix, and everytime +## thereafter return a cached version of the matrix until another +## matrix is set. -## Write a short comment describing this function +############################################################################# +## makeCacheMatrix: This function provides access to set and get a cached +## version of a matrix. -makeCacheMatrix <- function(x = matrix()) { +## x | matrix | The matrix to be set +## return list containing different functions used to get/set matrix +############################################################################# +makeCacheMatrix <- function(x = matrix()) { + im <- NULL + set <- function(y) { + x <<- y + im <<- NULL + } + get <- function() x + setsolve <- function(inMatrix) im <<- inMatrix + getsolve <- function() im + list(set = set, get = get, + setsolve = setsolve, + getsolve = getsolve) } +############################################################################# +## cacheSolve: This function returns an inverse of the matrix given to the makeCacheMatrix +## function. If the matrix has already been inverted and cached, it will return +## the cached version, otherwise, it will invert and then cache the matrix. -## Write a short comment describing this function +## x | list | The list of functions used to get/set matrix +## return inverted matrix +############################################################################# cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + m <- x$getsolve() + if(!is.null(m)) { + message("getting cached matrix") + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setsolve(m) + m }