Skip to content

Commit 34be08c

Browse files
committed
Commiting the final solution
1 parent 7f657dd commit 34be08c

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

cachematrix.R

Lines changed: 28 additions & 8 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
1+
## The below two functions caches, computes the
2+
## inverse of a matrix.
33

4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
4+
## makeCacheMatrix creates a matrix object
5+
## that can cache its inverse.
76

7+
makeCacheMatrix <- function(mtx = matrix()) {
8+
inverse <- NULL
9+
set <- function(x) {
10+
mtx <<- x;
11+
inverse <<- NULL;
12+
}
13+
get <- function() return(mtx);
14+
setinv <- function(inv) inverse <<- inv;
15+
getinv <- function() return(inverse);
16+
return(list(set = set, get = get, setinv = setinv, getinv = getinv))
817
}
918

1019

11-
## Write a short comment describing this function
20+
## cacheSolve function computes the inverse of the
21+
## matrix returned by makeCacheMatrix. If the inverse has
22+
## already been calculated (and the matrix has not changed), then
23+
## cacheSolve should retrieve the inverse from the cache.
1224

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

0 commit comments

Comments
 (0)