forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05bf4b3
commit b8a8eb2
Showing
2 changed files
with
45 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,35 @@ | ||
## [Put comments here that describe what your functions do] | ||
## Caching the Inverse of a Matrix: | ||
## Instead of computing the inverse of a matrix repeatedly, it is more beneficial to cache the inverse of a matrix since the computation of a matrix is usually a costly computation | ||
|
||
makeCacheMatrix <- function(x = matrix()) { | ||
## This function 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 | ||
setInverse <- function(inverse) inv <<- inverse | ||
getInverse <- function() inv | ||
list(set = set, | ||
get = get, | ||
setInverse = setInverse, | ||
getInverse = getInverse) | ||
} | ||
|
||
## The following function calculates the inverse of the special matrix created by the above function. However, it firs checks to see if the inverse has already been calculated. If so, it gets the inverse from the cache and skips the computation. Otherwise, it calculates the inverse of the data and sets the value of the minverse in the cache via the setInverse function. | ||
|
||
cacheSolve <- function(x, ...) { | ||
## Return a matrix that is the inverse of 'x' | ||
inv <- x$getInverse() | ||
if (!is.null(inv)) { | ||
message("getting cached data") | ||
return(inv) | ||
} | ||
mat <- x$get() | ||
inv <- solve(mat, ...) | ||
x$setInverse(inv) | ||
inv | ||
} | ||
|