forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
34 lines (29 loc) · 1.04 KB
/
cachematrix.R
File metadata and controls
34 lines (29 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
## This program contains a pair of functions that facilitate creating a matrix object and solving for it
## while utilizing the caching mechanism in R.
## makeCacheMatrix - Takes a matrix as an argument and builds the supporting functions for caching
makeCacheMatrix <- function(x = matrix()) {
inverse <- NULL
set <- function(y) {
x <<- y
inverse <<- NULL
}
get <- function() x
setInverse <- function(inv) inverse <<- inv
getInverse <- function() inverse
list(set=set, get=get, setInverse=setInverse, getInverse=getInverse)
}
## cacheSolve - Takes a first argument 'x' which is a cached matrix and solves it. It will use the cached value if available. The additional arguments
## are passed to the solve function if they are provided.
cacheSolve <- function(x, ...) {
inverse <- x$getInverse()
# Check if a cached value exists
if(!is.null(inverse)) {
message("Returning cached data.")
return(inverse)
}
# No cached value found, proceed with the calculation
data <- x$get()
inverse <- solve(data,...)
x$setInverse(inverse)
inverse
}