Skip to content

Commit 6667688

Browse files
committed
completing the code stubs from assignment
1 parent 7f657dd commit 6667688

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

cachematrix.R

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
# We compute the inverse of a matrix. Once we compute it, we cache it, so, that
2+
# don't need to compute it again. We can just retrieve from the cache.
33

4-
## Write a short comment describing this function
54

5+
# This function takes a matrix and makes an object that will cache its inverse.
66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
m <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
m <<- NULL
11+
}
12+
get <- function() x
13+
setsolve <- function(solve) m <<- solve
14+
getsolve <- function() m
15+
list(set = set, get = get,
16+
setsolve = setsolve,
17+
getsolve = getsolve)
818
}
919

1020

11-
## Write a short comment describing this function
12-
21+
# This function computes the inverse of a matrix. If it's already been
22+
# calculated, then the function will retrieve the cached inverse.
1323
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
24+
m <- x$getsolve()
25+
if(!is.null(m)) {
26+
message("getting cached data")
27+
return(m)
28+
}
29+
data <- x$get()
30+
m <- solve(data, ...)
31+
x$setsolve(m)
32+
m
1533
}

0 commit comments

Comments
 (0)