Skip to content

Commit 344cbe9

Browse files
committed
Matrix Cache Control Functions
1 parent 7f657dd commit 344cbe9

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

cachematrix.R

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
11
## Put comments here that give an overall description of what your
22
## functions do
33

4+
## makeCacheMatrix creates a list of functions to control the cached values within the cacheSolve function.
5+
6+
47
## Write a short comment describing this function
8+
## This function gets the matrix passed and store into the cache for future use, returning a list of functions to handle it.
59

610
makeCacheMatrix <- function(x = matrix()) {
7-
11+
m <- NULL
12+
set <- function(y) {
13+
x <<- y
14+
m <<- NULL
15+
}
16+
get <- function() x
17+
setsolve <- function(solve) m <<- solve
18+
getsolve <- function() m
19+
list(set = set, get = get,
20+
setsolve = setsolve,
21+
getsolve = getsolve)
822
}
923

1024

1125
## Write a short comment describing this function
26+
## This function verifies if the cache is set and uses it's value. If not, recalculate the matrix solve.
1227

1328
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
29+
## Return a matrix that is the inverse of 'x'
30+
m <- x$getsolve()
31+
if(!is.null(m)) {
32+
message("getting cached data")
33+
return(m)
34+
}
35+
data <- x$get()
36+
m <- solve(data, ...)
37+
x$setsolve(m)
38+
m
1539
}

0 commit comments

Comments
 (0)