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
38 lines (35 loc) · 1.05 KB
/
cachematrix.R
File metadata and controls
38 lines (35 loc) · 1.05 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
## These functions create an R object that maintains a
## matrix with a cacheable inverse and the equivalent
## solve function for calculating (and caching) the inverse
## makeCacheMatrix takes a square matrix x and returns
## a list that contains get/set functions for the
## matrix itself and its inverse
makeCacheMatrix <- function(x = matrix()) {
minv <- NULL
set <- function(y) {
x <<- y
minv <<- NULL
}
get <- function() x
setinv <- function(inv) minv <<- inv
getinv <- function() minv
list(set=set, get=get, setinv=setinv, getinv=getinv)
}
## cacheSolve takes an R object (list) created by
## makeCacheMatrix and returns its inverse.
## Internally, if the inverse has already been
## calculated it returns the cached value otherwise
## it calculates the inverse and caches for future
## calls
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
minv <- x$getinv()
if(!is.null(minv)) {
message("getting cached data")
return(minv)
}
m <- x$get()
minv <- solve(m, ...)
x$setinv(minv)
minv
}