Skip to content

Commit 3116ae2

Browse files
committed
implemented cache matrix
1 parent 7f657dd commit 3116ae2

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

cachematrix.R

Lines changed: 23 additions & 8 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
1+
## functions for creating a cacheable matrix and solving it.
52

3+
## creates a cacheable matrix
64
makeCacheMatrix <- function(x = matrix()) {
7-
5+
inv <- NULL
6+
set <- function(y) {
7+
x <<- y
8+
inv <<- NULL
9+
}
10+
get <- function() x
11+
setinv <- function(y) inv <<- y
12+
getinv <- function() inv
13+
list(set = set, get = get,
14+
setinv = setinv,
15+
getinv = getinv)
816
}
917

1018

11-
## Write a short comment describing this function
12-
19+
## solves for the inverse of a cacheable matrix (or uses the cached solution)
1320
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
21+
inv <- x$getinv()
22+
if(!is.null(inv)) {
23+
message("getting cached data")
24+
return(inv)
25+
}
26+
data <- x$get()
27+
inv <- solve(data, ...)
28+
x$setinv(inv)
29+
inv
1530
}

0 commit comments

Comments
 (0)