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