File tree Expand file tree Collapse file tree 1 file changed +29
-8
lines changed Expand file tree Collapse file tree 1 file changed +29
-8
lines changed Original file line number Diff line number Diff line change 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
5
3
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
6
6
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 )
8
20
}
9
21
10
22
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
13
25
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
15
36
}
You can’t perform that action at this time.
0 commit comments