-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathcachematrix.R
48 lines (43 loc) · 1.32 KB
/
cachematrix.R
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
## cachematrix.R
##
## Create a cache marix object that can be used to
## repeatably solve the inverse of the marix, but only
## calculates the inverse once.
##
## Usage:
## M <- matrix(c(1, 2, 3, 4), nrow=2, ncol=2)
## cacheMatrix <- makeCacheMatrix(M)
## cacheSolve(cacheMatrix)
##
## cacheMatrix$set(M) # Change the matrix being cached.
## M <- cacheMatrix$get() # Returns the matrix being cached.
##
## cacheMatrix$setInverse(solve(data, ...)) # Private function containing cached inverse of x
## cacheMatrix$getInverse() # Private function used to get the cached inverse of x
## Create a cacheMatrix object for an invertale matrix.
makeCacheMatrix <- function(x = matrix()) {
cachedInverse <- NULL
set <- function(y) {
x <<- y
cachedInverse <<- NULL
}
get <- function() x
setInverse <- function(inverse) cachedInverse <<- inverse
getInverse <- function() cachedInverse
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## Return the inverse of an cacheMatrix object
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
invFunc <- x$getInverse()
if(!is.null(invFunc)) {
message("getting cached data")
return(invFunc)
}
data <- x$get()
invFunc <- solve(data, ...)
x$setInverse(invFunc)
invFunc
}