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

Skip to content

Commit 27edb8b

Browse files
committed
Commit for the programming assignment rdpeng#2
1 parent 7f657dd commit 27edb8b

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

cachematrix.R

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
11
## Put comments here that give an overall description of what your
22
## functions do
33

4-
## Write a short comment describing this function
4+
## This function creates a special "matrix" object that can cache its inverse.
55

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
21+
## This function computes the inverse of the special "matrix" returned by makeCacheMatrix above. If the inverse has already been calculated (and the matrix has not changed), then the cachesolve should retrieve the inverse from the cache.
1222

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

0 commit comments

Comments
 (0)