Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 77024e5

Browse files
committed
memorize the solve function
1 parent 7f657dd commit 77024e5

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

cachematrix.R

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
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+
## Memorize the solve() function for matrices by building an object with
2+
## makeCacheMatrix and using the object with cacheSolve
53

4+
## return a list of functions to get and set a matrix
5+
## and 2 functions to get and get an inverse of the matrix
66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
inv <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
inv <<- NULL
11+
}
12+
get <- function() x
13+
14+
setinv <- function(inverse) inv <<- inverse
15+
getinv <- function() inv
16+
17+
list(set = set, get = get,
18+
setinv = setinv,
19+
getinv = getinv)
820
}
921

1022

11-
## Write a short comment describing this function
12-
23+
## Use the list created from makeCacheMatrix and check for a cached value
24+
## if not found calculate it and set the value
1325
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
26+
inv <- x$getinv()
27+
if(!is.null(inv)) {
28+
message("getting cached value")
29+
return(inv)
30+
}
31+
32+
data <- x$get()
33+
inv <- solve(data, ...)
34+
x$setinv(inv)
35+
inv
1536
}

0 commit comments

Comments
 (0)