forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
30 lines (24 loc) · 775 Bytes
/
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
## Introduces special matrice that cache its inverse
## makeCacheMatrix creates a list with 4 functions: set, get, setinv, getinv
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setinv <- function(i) inv <<- i
getinv <- function() inv
list(set = set, get = get, setinv = setinv, getinv = getinv)
}
## returns a cached inverse of a matrix if precomputed, computes otherwise
cacheSolve <- function(x, ...) {
inv <- x$getinv()
if (is.null(inv)) {
inv <- solve(x$get(), ...)
x$setinv(inv)
} else {
message("getting cached data")
}
inv
}