Skip to content

Commit 022c2ef

Browse files
committed
Solve prog. assignment 2
1 parent 7f657dd commit 022c2ef

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
3-
4-
## Write a short comment describing this function
1+
## Pair of function that compute/cache the inverse of a matrix.
52

3+
## This function creates a special "matrix" object that can cache its inverse.
64
makeCacheMatrix <- function(x = matrix()) {
5+
s <- NULL
6+
setMatrix <- function(y) {
7+
x <<- y
8+
s <<- NULL
9+
}
10+
getMatrix <- function() x
11+
setSolve <- function(sol) s <<- sol
12+
getSolve <- function() s
13+
list(setMatrix = setMatrix, getMatrix = getMatrix,
14+
setSolve = setSolve, getSolve = getSolve)
715

816
}
917

1018

11-
## Write a short comment describing this function
12-
19+
## This function computes the inverse of the special "matrix" returned by
20+
## makeCacheMatrix above. If the inverse has already been calculated (and
21+
## the matrix has not changed), then cacheSolve should retrieve the inverse
22+
## from the cache.
1323
cacheSolve <- function(x, ...) {
1424
## 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$getMatrix()
31+
s <- solve(data, ...)
32+
x$setSolve(s)
33+
s
1534
}

0 commit comments

Comments
 (0)