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
31 lines (25 loc) · 698 Bytes
/
Copy pathcachematrix.R
File metadata and controls
31 lines (25 loc) · 698 Bytes
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
## This functions cache the inverse of a matrix.
## Creates a special "matrix" object that can cache its inverse.
makeCacheMatrix <- function(x = matrix()) {
mtrx_inv <- NULL
set <- function(y) {
x <<- y
mtrx_inv <<- NULL
}
get <- function() x
setinv <- function(inv) mtrx_inv <<- inv
getinv <- function() mtrx_inv
list(set = set, get = get, setinv= setinv, getinv = getinv)
}
## Computes the inverse or retrieves the inverse from the cache.
cacheSolve <- function(x, ...) {
mtrx_inv <- x$getinv()
if(!is.null(mtrx_inv)) {
message("getting cached data")
return(mtrx_inv)
}
data <- x$get()
mtrx_inv <- solve(data, ...)
x$setinv(mtrx_inv)
mtrx_inv
}