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
+ # # makeCacheMatrix and cacheSolve allow you to calculate the inverse
2
+ # # of a matrix and cache the result
5
3
4
+ # # Create a data structure containing a matrix and, optionally, its inverse
5
+ # # and return getters and setters for both.
6
6
makeCacheMatrix <- function (x = matrix ()) {
7
-
7
+ xinverse <- NULL
8
+ set <- function (y ) {
9
+ x <<- y
10
+ xinverse <<- NULL
11
+ }
12
+ get <- function () x
13
+ setinverse <- function (inverse ) xinverse <<- inverse
14
+ getinverse <- function () xinverse
15
+ list (set = set , get = get ,
16
+ setinverse = setinverse ,
17
+ getinverse = getinverse )
8
18
}
9
19
10
20
11
- # # Write a short comment describing this function
12
-
13
- cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
21
+ # # Given a CacheMatrix return or compute and cache the inverse of the matrix.
22
+ cacheSolve <- function (x ) {
23
+ xinverse <- x $ getinverse()
24
+ if (! is.null(xinverse )) {
25
+ message(" getting cached data" )
26
+ return (xinverse )
27
+ }
28
+ matrix <- x $ get()
29
+ xinverse <- solve(matrix )
30
+ x $ setinverse(xinverse )
31
+ xinverse
15
32
}
You can’t perform that action at this time.
0 commit comments