Skip to content

Commit 483a2cb

Browse files
committed
Complete the functions.
1 parent 7f657dd commit 483a2cb

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

cachematrix.R

Lines changed: 29 additions & 9 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
3-
4-
## Write a short comment describing this function
5-
1+
## funtcion makeCacheMatrix() creates a special "matrix" object that can cache its inverse.
62
makeCacheMatrix <- function(x = matrix()) {
7-
3+
m <- NULL
4+
set <- function(y) {
5+
x <<- y
6+
m <<- NULL
7+
}
8+
get <- function() x
9+
setInverse <- function(solve) m <<- solve
10+
getInverse <- function() m
11+
list(set = set, get = get,
12+
setInverse = setInverse,
13+
getInverse = getInverse)
814
}
915

16+
## cacheSolve() computes the inverse of the special "matrix" returned by makeCacheMatrix above.
17+
## If the inverse has already been calculated (and the matrix has not changed), then the cachesolve
18+
## should retrieve the inverse from the cache.
19+
cacheSolve <- function(x, ...) {
20+
m <- x$getInverse()
1021

11-
## Write a short comment describing this function
22+
## Return a matrix that is the inverse of 'x' if the inverse has already been calculated and cached.
23+
if(!is.null(m)) {
24+
message("getting cached data")
25+
return(m)
26+
}
1227

13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
28+
## Calculate the inverse of 'x' if the cached data doesn't exist.
29+
data <- x$get()
30+
m <- solve(data, ...)
31+
x$setInverse(m)
32+
33+
## Return a matrix that is the inverse of 'x'
34+
m
1535
}

0 commit comments

Comments
 (0)