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 (39 loc) · 1.52 KB
/
cachematrix.R
File metadata and controls
44 lines (39 loc) · 1.52 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
## Calculating inverse matrices can be slow. To save time, this pair of
## functions creates a list to store the matrix, it's inverse and methods
## to get and set the matrix, calculate it's inverse, and retrieve the
## cached inverse. 'makeCacheMatrix' should be called to create the cached
## matrix. 'cacheSolve' can then be called to get the inverse.
## This function creates a matrix-like object capable of storing it's own
## inverse, so that we can save time by only calculating the inverse of the
## matrix once.
## Returns a list containing methods to get and set the matrix and the inverse
makeCacheMatrix <- function(x = matrix()) {
inverse <- NULL
set <- function(y) {
x <<- y
inverse <<- NULL
}
get <- function() x
setinverse <- function(solve) inverse <<- solve
getinverse <- function() inverse
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## This function takes a cacheMatrix as created by the above function,
## 'makeCacheMatrix'. It checks to see if the matrix has already been
## solved. If it has, it returns the cached value. If not, it calculates
## the inverse, caches it, and returns the inverse.
## Returns a matrix -- the inverse of the matrix passed to 'makeCacheMatrix'
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inverse <- x$getinverse();
if (!is.null(inverse)) {
message("getting cached data")
return(inverse)
}
data <- x$get()
inverse <- solve(data, ...)
x$setinverse(inverse)
inverse
}