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