File tree Expand file tree Collapse file tree 1 file changed +29
-9
lines changed Expand file tree Collapse file tree 1 file changed +29
-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
+ # My solution to Programming Assignment 2
5
2
6
3
makeCacheMatrix <- function (x = matrix ()) {
7
-
4
+ inv <- NULL # Store the inverse
5
+
6
+ set <- function (y ) {
7
+ x <<- y # Set new matrix
8
+ inv <<- NULL # Clear cached inverse
9
+ }
10
+
11
+ get <- function () x
12
+
13
+ setinverse <- function (inverse ) inv <<- inverse
14
+
15
+ getinverse <- function () inv
16
+
17
+ list (set = set , get = get ,
18
+ setinverse = setinverse ,
19
+ getinverse = getinverse )
8
20
}
9
21
10
-
11
- # # Write a short comment describing this function
12
-
13
22
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
23
+ inv <- x $ getinverse()
24
+ if (! is.null(inv )) {
25
+ message(" getting cached inverse" )
26
+ return (inv )
27
+ }
28
+
29
+ mat <- x $ get()
30
+ inv <- solve(mat , ... ) # Compute inverse
31
+ x $ setinverse(inv )
32
+ inv
15
33
}
34
+
35
+ # Submitted by Aliya-yuan
You can’t perform that action at this time.
0 commit comments