Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d6db7bb

Browse files
committed
Implement makeCacheMatrix and cacheSolve
1 parent 7f657dd commit d6db7bb

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

cachematrix.R

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Gives the inverse of a given matrix, caching the result
2+
## and using the cached result if available
33

4-
## Write a short comment describing this function
4+
## Builds an object that can store and retrieve a cached matrix inverse
55

66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
i <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
i <<- NULL
11+
}
12+
get <- function() x
13+
setinv <- function(inv) i <<- inv
14+
getinv <- function() i
15+
list(set = set, get = get,
16+
setinv = setinv,
17+
getinv = getinv)
818
}
919

1020

11-
## Write a short comment describing this function
21+
## Returns the inverse of a special matrix
22+
## Uses cached value if available, calculates and caches if not
1223

1324
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
25+
i <- x$getinv()
26+
if(!is.null(i)) {
27+
message("getting cached data")
28+
return(i)
29+
}
30+
data <- x$get()
31+
i <- solve(data, ...)
32+
x$setinv(i)
33+
i
1534
}

0 commit comments

Comments
 (0)