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

Skip to content

Commit db437bc

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

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

cachematrix.R

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
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+
## The following functions can create matrix which will cache its inverse
2+
## for quick "calculation" in future calls.
53

4+
## Make a matrix which can store a value for its inverse
5+
## instead of solving for it every time.
66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
s <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
s <<- NULL
11+
}
12+
get <- function() x
13+
setsolve <- function(solve) s <<- solve
14+
getsolve <- function() s
15+
list(set = set, get = get,
16+
setsolve = setsolve,
17+
getsolve = getsolve)
818
}
919

1020

11-
## Write a short comment describing this function
12-
21+
## Calculate and cache the inverse of a matrix.
1322
cacheSolve <- function(x, ...) {
1423
## Return a matrix that is the inverse of 'x'
24+
s <- x$getsolve()
25+
if(!is.null(s)) {
26+
message("getting cached data")
27+
return(s)
28+
}
29+
data <- x$get()
30+
s <- solve(data, ...)
31+
x$setsolve(s)
32+
s
1533
}

0 commit comments

Comments
 (0)