File tree 1 file changed +25
-6
lines changed
1 file changed +25
-6
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
1
+ # # Gives the inverse of a given matrix, caching the result
2
+ # # and using the cached result if available
3
3
4
- # # Write a short comment describing this function
4
+ # # Builds an object that can store and retrieve a cached matrix inverse
5
5
6
6
makeCacheMatrix <- function (x = matrix ()) {
7
-
7
+ i <- NULL
8
+ set <- function (y ) {
9
+ x <<- y
10
+ i <<- NULL
11
+ }
12
+ get <- function () x
13
+ setinv <- function (inv ) i <<- inv
14
+ getinv <- function () i
15
+ list (set = set , get = get ,
16
+ setinv = setinv ,
17
+ getinv = getinv )
8
18
}
9
19
10
20
11
- # # Write a short comment describing this function
21
+ # # Returns the inverse of a special matrix
22
+ # # Uses cached value if available, calculates and caches if not
12
23
13
24
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
25
+ i <- x $ getinv()
26
+ if (! is.null(i )) {
27
+ message(" getting cached data" )
28
+ return (i )
29
+ }
30
+ data <- x $ get()
31
+ i <- solve(data , ... )
32
+ x $ setinv(i )
33
+ i
15
34
}
You can’t perform that action at this time.
0 commit comments