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
40 lines (35 loc) · 1.24 KB
/
cachematrix.R
File metadata and controls
40 lines (35 loc) · 1.24 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
## Matrix inversion can be a costly computation, and in some situations it may
## be benificial to cache the inverse of a matrix rather than compute it
## repeatedly. The following pair of functions provide this functionality. This
## code is a straightforward modification of the example code for caching the
## mean of a vector given in the assignment description.
## This function extends the "matrix" object with getters and setters that can
## cache its inverse.
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setinverse <- function(inverse) m <<- inverse
getinverse <- function() m
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## This function computes the inverse of a matrix using the extended "matrix"
## object returned by makeCacheMatrix. The cacheSolve function will retrieve
## the cached inverse if it has already been calculated and the matrix has not
## changed.
cacheSolve <- function(x, ...) {
m <- x$getinverse()
if(!is.null(m)) {
message("getting cached inverse")
return(m)
}
data <- x$get()
m <- solve(data, ...)
x$setinverse(m)
m
}