File tree Expand file tree Collapse file tree 1 file changed +32
-8
lines changed Expand file tree Collapse file tree 1 file changed +32
-8
lines changed Original file line number Diff line number Diff line change 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
+ # # Matrix inversion is usually a costly computation and there may be some benefit to caching the inverse of
2
+ # # a matrix rather than compute it repeatedly. Below are a pair of functions that cache the inverse of a matrix.
5
3
4
+ # # makeCacheMatrix:
5
+ # # This function creates a special "matrix" object that can cache its inverse.
6
+ # #
6
7
makeCacheMatrix <- function (x = matrix ()) {
7
-
8
+ invMatrix <- NULL
9
+ set <- function (y ) {
10
+ x <<- y
11
+ invMatrix <<- NULL
12
+ }
13
+ get <- function () x
14
+ setInverse <- function (inv ) invMatrix <<- inv
15
+ getInverse <- function () invMatrix
16
+ list (set = set ,
17
+ get = get ,
18
+ setInverse = setInverse ,
19
+ getInverse = getInverse )
8
20
}
9
21
10
22
11
- # # Write a short comment describing this function
12
-
23
+ # # cacheSolve:
24
+ # # This function computes the inverse of the special "matrix" returned by makeCacheMatrix.
25
+ # # If the inverse has already been calculated (and the matrix has not changed),
26
+ # # then cacheSolve should retrieve the inverse from the cache.
27
+ # #
13
28
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
29
+ invMatrix <- x $ getInverse()
30
+ if (! is.null(invMatrix )) {
31
+ message(" getting cached matrix" )
32
+ return (invMatrix )
33
+ }
34
+ m <- x $ get()
35
+ # # Return a matrix that is the inverse of 'm'
36
+ invMatrix <- solve(m , ... )
37
+ x $ setInverse(invMatrix )
38
+ invMatrix
15
39
}
You can’t perform that action at this time.
0 commit comments