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
35 lines (34 loc) · 707 Bytes
/
cachematrix.R
File metadata and controls
35 lines (34 loc) · 707 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
31
32
33
34
35
# makeCacheMatrix function
# input: matrix
# output: list of 4 functions: get, set, getinv, setinv
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(
getinv = getinv,
setinv = setinv,
get = get,
set = set
)
}
# Cachesolve function
# input: list from makeCacheMatrix
# output: matrix inverse
# trying to find cached solution first, if none - solve & cache
cacheSolve <- function(x, ...) {
i <- x$getinv()
if (!is.null(i)) {
message("Getting cached inverse")
return(i)
}
m <- x$get()
i <- solve(m)
x$setinv(i)
i
}