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
39 lines (32 loc) · 1.14 KB
/
cachematrix.R
File metadata and controls
39 lines (32 loc) · 1.14 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
## This pair of functions will return the inverse of a matrix.
## If the inverse has not been calculated previously, it
## calculates it from scratch and caches the result.
## If the inverse has been calculated previously, it returns
## the cached inverse instead of calculating it again.
## makeCacheMatrix creates a list of 3 functions:
## get() is just the original matrix
## setinv() caches the value of the inverse
## getinv() gets the cached value of the inverse or returns
## NULL if not cached
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
get <- function() x
setinv <- function(inv) m <<- inv
getinv <- function() m
list(get = get, setinv = setinv, getinv = getinv)
}
## cacheSolve first looks for a cached inverse. If it finds
## one, it returns that cached value. If it doesn't find one,
## it calculates the inverse, then caches and returns it.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
m <- x$getinv()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data, ...)
x$setinv(m)
m
}