File tree Expand file tree Collapse file tree 1 file changed +29
-7
lines changed Expand file tree Collapse file tree 1 file changed +29
-7
lines changed Original file line number Diff line number Diff line change 1
1
# # Put comments here that give an overall description of what your
2
2
# # functions do
3
3
4
- # # Write a short comment describing this function
5
-
4
+ # # This function creates a special "matrix" object
5
+ # # that can cache its inverse.
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
+ setinverse <- function (inverse ) inv <<- inverse
14
+ getinverse <- function () inv
15
+
16
+ list (set = set , get = get ,
17
+ setinverse = setinverse ,
18
+ getinverse = getinverse )
8
19
}
9
20
10
21
11
- # # Write a short comment describing this function
12
-
22
+ # # This function computes the inverse of the special
23
+ # #"matrix" returned by `makeCacheMatrix` above. If the inverse has
24
+ # #already been calculated (and the matrix has not changed), then
25
+ # #this function` retrieves the inverse from the cache.
13
26
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
15
- }
27
+ # # Return a matrix that is the inverse of 'x'
28
+ inv <- x $ getinverse()
29
+ if (! is.null(inv )) {
30
+ message(" getting cached data" )
31
+ return (inv )
32
+ }
33
+ data <- x $ get()
34
+ inv <- solve(data , ... )
35
+ x $ setinverse(inv )
36
+ inv
37
+ }
You can’t perform that action at this time.
0 commit comments