forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcachematrix.R
More file actions
43 lines (40 loc) · 1.64 KB
/
cachematrix.R
File metadata and controls
43 lines (40 loc) · 1.64 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
37
38
39
40
41
42
43
## makeCacheMatrix creates a special "matrix" object that can
## cache its inverse. cacheSolve computes the inverse of the special "matrix"
## returned by makeCacheMatrix. If the inverse has already been calculated, it will find
## it in the cache and return it rather than calculating it again.
## This function creates a list containing a function to:
## 1. set the value of the matrix
## 2. get the value of the matrix
## 3. set the value of the inverse
## 4. get the value of the inverse
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setInv <- function(inverse) inv <<- inverse
getInv <- function() inv
list(set = set, get = get,
setInv = setInv,
getInv = getInv)
}
## This function computes the inverse of the special matrix created by makeCacheMatrix.
## If the inverse has already been calculated it retrieves it from the cache, otherwise
## it will compute it and return it.
cacheSolve <- function(x, ...) {
inv <- x$getInv()
#If the inverse is available retrieve it from cache
if(!is.null(inv)) {
message("Getting cached data")
return(inv)
}
#Otherwise compute and return the inverse
else {
data <- x$get()
inv <- solve(data)
x$setInv(inv)
inv
}
}