File tree Expand file tree Collapse file tree 1 file changed +29
-5
lines changed Expand file tree Collapse file tree 1 file changed +29
-5
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
+ # # Matrix inversion is usually a costly computation and there may be some benefit to caching
2
+ # #the inverse of a matrix rather than compute it repeatedly (there are also alternatives to
3
+ # #matrix inversion that we will not discuss here). Your assignment is to write a pair of
4
+ # #functions that cache the inverse of a matrix.
3
5
4
- # # Write a short comment describing this function
6
+ # # This function creates (returns) a special "matrix" object that can cache its inverse.
5
7
6
8
makeCacheMatrix <- function (x = matrix ()) {
7
-
9
+ i <- NULL
10
+ set <- function (y ) {
11
+ x <<- y
12
+ i <<- NULL
13
+ }
14
+ get <- function () x
15
+ setinv <- function (solve ) i <<- solve
16
+ getinv <- function () i
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
23
+ # # This function computes the inverse of the special "matrix" returned by makeCacheMatrix above.
24
+ # # If the inverse has already been calculated (and the matrix has not changed),
25
+ # # then the cachesolve should retrieve the inverse from the cache.
12
26
13
27
cacheSolve <- function (x , ... ) {
14
28
# # Return a matrix that is the inverse of 'x'
29
+ i <- x $ getinv()
30
+ if (! is.null(i )) {
31
+ message(" getting cached data" )
32
+ return (i )
33
+ }
34
+ data <- x $ get()
35
+ i <- solve(data , ... )
36
+ x $ setinv(i )
37
+ i
15
38
}
39
+
You can’t perform that action at this time.
0 commit comments