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

Skip to content

Commit d5e6166

Browse files
committed
implemented functions makeCacheMatrix and cacheSolve
1 parent 7f657dd commit d5e6166

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

cachematrix.R

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Functions to solve the inverse of a matrix and cache the result.
32

4-
## Write a short comment describing this function
3+
## Creates a container that can hold a matrix and its cached inverse.
54

65
makeCacheMatrix <- function(x = matrix()) {
7-
6+
xinv <- NULL
7+
set <- function(y) {
8+
xinv <<- NULL
9+
x <<- y
10+
}
11+
list(set = set, get = function() x,
12+
setinverse = function(y) xinv <<- y, getinverse = function() xinv)
813
}
914

10-
11-
## Write a short comment describing this function
15+
## Solves and caches the inverse of the matrix, so that repeated calls return the cached result until the matrix is changed.
1216

1317
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
18+
xinv <- x$getinverse()
19+
if (is.null(xinv)) {
20+
message("solving for the inverse")
21+
xinv <- solve(x$get(), ...)
22+
x$setinverse(xinv)
23+
} else {
24+
message("using cached solution")
25+
}
26+
xinv
1527
}
28+

0 commit comments

Comments
 (0)