forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
46 lines (29 loc) · 1.03 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
39
40
41
42
43
44
45
46
## The makeCacheMatrix creates special matrix object that is capable of caching its mean
## The cacheSolve finds an inverse of a matrix. It returns cached result when possible
## Creates matrix that is capable of remembering its inverse
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(m) {
x <<- m
inv <<- NULL
}
get <- function() x
setInv <- function(i) inv <<- i
getInv <- function() inv
list(get = get, set = set, getInv = getInv, setInv = setInv)
}
## Calculates inverse. It may use cached value if inverse was previously calculated.
## The x argument is a matrix created using makeCahceMatrix
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getInv()
if(!is.null(inv))
{
message("inverse from cache")
return(inv)
}
matrix <- x$get()
inv <- solve(matrix)
x$setInv(inv)
inv
}