Skip to content

Commit b4f5c09

Browse files
committed
add code for makeCacheMatrix and cacheSolve functions
1 parent 7f657dd commit b4f5c09

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

cachematrix.R

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
61
makeCacheMatrix <- function(x = matrix()) {
7-
2+
inverse <- NULL
3+
set <- function(y) {
4+
x <<- y
5+
inverse <<- NULL
6+
}
7+
get <- function() x
8+
setinverse <- function(inv) inverse <<- inv
9+
getinverse <- function() inverse
10+
list(set = set, get = get,
11+
setinverse = setinverse,
12+
getinverse = getinverse)
813
}
914

1015

11-
## Write a short comment describing this function
12-
1316
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
17+
## Return a matrix that is the inverse of 'x'
18+
inverse <- x$getinverse()
19+
if(!is.null(inverse)) {
20+
message("getting cached inverse matrix")
21+
return(inverse)
22+
}
23+
24+
data <- x$get()
25+
message("computing inverse matrix")
26+
inverse <- solve(data, ...)
27+
x$setinverse(inverse)
28+
inverse
1529
}
30+

0 commit comments

Comments
 (0)