Skip to content

Commit 66cbbe9

Browse files
authored
Add files via upload
1 parent 7f657dd commit 66cbbe9

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

cachematrix.R

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
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
52

63
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)
820
}
921

10-
11-
## Write a short comment describing this function
12-
1322
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
1533
}
34+
35+
# Submitted by Aliya-yuan

0 commit comments

Comments
 (0)