Skip to content

Commit fb058a0

Browse files
committed
Completed both makeCacheMatrix and cacheSolve functions.
1 parent 7f657dd commit fb058a0

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

cachematrix.R

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
1+
## makeCacheMatrix returns an object that can cache the inverse of a specified matrix.
52

63
makeCacheMatrix <- function(x = matrix()) {
7-
4+
inv <- NULL
5+
set <- function(m) {
6+
x <<- m
7+
inv <<- NULL
8+
}
9+
get <- function() x
10+
setInverse <- function(i) inv <<- i
11+
getInverse <- function() inv
12+
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
813
}
914

1015

11-
## Write a short comment describing this function
16+
## cacheSolve asks if the inverse is null, meaning it hasn't been cached yet, and computes it if so.
17+
## Otherwise, it returns the cached inverse. This function passes any additional arguments on to solve.
1218

1319
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
20+
if (is.null(x$getInverse())) {
21+
i <- solve(x$get(), ...)
22+
x$setInverse(i)
23+
return(i)
24+
}
25+
x$getInverse()
1526
}

0 commit comments

Comments
 (0)