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