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
41 lines (32 loc) · 1.1 KB
/
cachematrix.R
File metadata and controls
41 lines (32 loc) · 1.1 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
35
36
37
38
39
40
41
## This is an implementation of a matrix together with its cached inverse
## makeCacheMatrix creates a matrix "object" capable of caching its inverse
makeCacheMatrix <- function(x = matrix()) {
cached <- NULL
## create getter and setter functions for the matrix and its cached inverse
set <- function(y) {
x <<- y
cached <<- NULL
}
get <- function() x
setInverse <- function(mean) cached <<- mean
getInverse <- function() cached
## return the methods for this "object"
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## cacheSolve returns the inverse of a cachematrix object,
## using a cached result of the inverse has been computed already
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
## if there is a cached inverse, return it
cached <- x$getInverse()
if (!is.null(cached)) {
return(cached)
}
## compute the inverse, cache it and return it
data <- x$get()
cached <- solve(data, ...)
x$setInverse(cached)
cached
}