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