-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
36 lines (30 loc) · 1.12 KB
/
cachematrix.R
File metadata and controls
36 lines (30 loc) · 1.12 KB
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
## Following includes both a function to create a 'cached matrix object' capable to
## remember an inverse matrix of itself, and a function 'cacheSolve'
## to calculate inverse matrix of this 'cached matrix object'.
## Returns a list describing a matrix object capable of caching
## own inverse matrix.
makeCacheMatrix <- function(mat = matrix()) {
inverse <- NULL
set <- function(newMat) {
mat <<- newMat
inverse <<- NULL
}
get <- function() mat
setInv <- function(inv) inverse <<- inv
getInv <- function() inverse
list(set = set, get = get, setInv = setInv, getInv = getInv)
}
## Returns inverse of matrix 'cacheMat' and recalculates the
## inverse matrix only if it has not been calculated already.
cacheSolve <- function(cacheMat, ...) {
## Return a matrix that is the inverse of 'cacheMat'
inv <- cacheMat$getInv()
if (!is.null(inv)) {
message("getting cached data")
return (inv)
}
mat <- cacheMat$get()
inv <- solve(mat, ...)
cacheMat$setInv(inv)
inv
}