forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
38 lines (33 loc) · 1.37 KB
/
cachematrix.R
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
# functions 'makeCacheatrix' and 'cacheSolve' allow caching of a matrix' inverse,
# and finding the inverse by returning the cached value if it exists.
# function 'makeCacheMatrix' gets as input matrix 'x' and outputs an object which is a
# list of four functions: 'set' and 'get' to set and get the matrix value, and 'setinv'
# and 'getinv' to set and get the matrix' inverse.
# once the inverse is set, it caches its value for future use.
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
inv <<- NULL
x <<- y
}
get <- function() x
setinv <- function(inverse) inv <<- inverse
getinv <- function() inv
list(set=set, get=get, setinv=setinv, getinv=getinv)
}
# function 'cacheSolve' gets as value a cached matrix 'x' (an object created
# by 'makeCacheMatrix' function), and returns its inverse. if the inverse was
# not computed beforehand, it computes its value and caches it in 'x'. if it
# was computed, it returns the cached value.
cacheSolve <- function(x, printMsg=T, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getinv()
if (!is.null(inv)) {
if (printMsg==T) message("getting cached data")
return(0)
}
data <- x$get()
inv <- solve(data)
x$setinv(inv)
inv
}