Skip to content

Commit 5964701

Browse files
author
Jesse Kochis
committed
Completed programming assignment 2
1 parent 7f657dd commit 5964701

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

cachematrix.R

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## These functions allow the program to run more efficiently by
2+
## caching certain computations that maybe costly. In this case,
3+
## it caches the inverse of a matrix to avoid computing again.
34

4-
## Write a short comment describing this function
5+
## This function creates a special "matrix" object that can cache
6+
## its inverse.
57

68
makeCacheMatrix <- function(x = matrix()) {
7-
9+
invX <- NULL
10+
set <- function(y) {
11+
x <<- y
12+
invX <<- NULL
13+
}
14+
get <- function() {
15+
x
16+
}
17+
setInv <- function(inv) {
18+
invX <<- inv
19+
}
20+
getInv <- function() {
21+
invX
22+
}
23+
list(set = set, get = get, setInv = setInv, getInv = getInv)
824
}
925

1026

11-
## Write a short comment describing this function
27+
## This function computes the inverse of the special "matrix"
28+
## returned by makeCacheMatrix above. If the inverse has already
29+
## been calculated (and the matrix has not changed), then
30+
## cacheSolve should retrieve the inverse from the cache.
1231

1332
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
33+
mat <- x$getInv()
34+
if(!is.null(mat)) {
35+
mat
36+
}
37+
data <- x$get()
38+
mat <- solve(data)
39+
x$setInv(mat)
40+
mat
1541
}

0 commit comments

Comments
 (0)