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
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
+ # # Creates a special matrix which can be used to cache the inverse of this matrix
6
5
makeCacheMatrix <- function (x = matrix ()) {
7
-
6
+ i <- NULL
7
+ set <- function (y ) {
8
+ # When setting a new matrix, the inverse needs to be cleared as well
9
+ x <<- y
10
+ i <<- NULL
11
+ }
12
+
13
+ get <- function () x
14
+ setInverse <- function (inverse ) i <<- inverse
15
+ getInverse <- function () i
16
+
17
+ list (set = set ,
18
+ get = get ,
19
+ getInverse = getInverse ,
20
+ setInverse = setInverse )
8
21
}
9
22
10
-
11
- # # Write a short comment describing this function
12
-
23
+ # # Returns a matrix that is the inverse of x. The result is cached so subsequent calls to this function
24
+ # # returns the same result without computing it again.
13
25
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
26
+ i <- x $ getInverse()
27
+
28
+ if (is.null(i )) {
29
+ # We need to do the computation
30
+ i <- solve(x $ get())
31
+ x $ setInverse(i )
32
+ }
33
+
34
+ i
15
35
}
You can’t perform that action at this time.
0 commit comments