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
44 lines (35 loc) · 1.29 KB
/
cachematrix.R
File metadata and controls
44 lines (35 loc) · 1.29 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
42
43
44
## These functions implement an object in R representing a matrix with the
## ability to cache its own inverse when using the cacheSolve function.
## makeCacheMatrix makes an R object to cache the inverse for a given matrix
makeCacheMatrix <- function(x = matrix()) {
# initially we do not know the inverse
i <- NULL
# define the functions this object implements
set <- function(y) {
x <<- y
# must reset cached inverse when x changes
i <<- NULL
}
get <- function() x
setinverse <- function(inverse) i <<- inverse
getinverse <- function() i
# return the object as a list
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## cacheSolve solves the inverse for the matrix object, unless it is cached
cacheSolve <- function(x, ...) {
# if it is cached, then just return it
i <- x$getinverse()
if(!is.null(i)) {
# for debugging: message("getting cached data")
return(i)
}
# otherwise, solve and cache it for future use
data <- x$get()
i <- solve(data, ...)
x$setinverse(i)
## Return a matrix that is the inverse of 'x'
i
}