Skip to content

Commit 31c50f4

Browse files
committed
Update cachematrix.R
1 parent 7f657dd commit 31c50f4

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

cachematrix.R

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Caching the Inverse of a Matrix:
2+
## Matrix inversion is usually a costly computation and there may be some
3+
## benefit to caching the inverse of a matrix rather than compute it repeatedly.
4+
## Below are a pair of functions that are used to create a special object that
5+
## stores a matrix and caches its inverse.
36

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

69
makeCacheMatrix <- function(x = matrix()) {
7-
10+
inv <- NULL
11+
set <- function(y) {
12+
x <<- y
13+
inv <<- NULL
14+
}
15+
get <- function() x
16+
setInverse <- function(inverse) inv <<- inverse
17+
getInverse <- function() inv
18+
list(set = set,
19+
get = get,
20+
setInverse = setInverse,
21+
getInverse = getInverse)
822
}
923

1024

11-
## Write a short comment describing this function
25+
## This function computes the inverse of the special "matrix" created by
26+
## makeCacheMatrix above. If the inverse has already been calculated (and the
27+
## matrix has not changed), then it should retrieve the inverse from the cache.
1228

1329
cacheSolve <- function(x, ...) {
1430
## Return a matrix that is the inverse of 'x'
15-
}
31+
inv <- x$getInverse()
32+
if (!is.null(inv)) {
33+
message("getting cached data")
34+
return(inv)
35+
}
36+
mat <- x$get()
37+
inv <- solve(mat, ...)
38+
x$setInverse(inv)
39+
inv
40+
}

0 commit comments

Comments
 (0)