Skip to content

Commit 7eeb6b2

Browse files
committed
finished makeCacheMatrix and cacheSolve functions
1 parent 7f657dd commit 7eeb6b2

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

cachematrix.R

Lines changed: 26 additions & 7 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+
## 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
55

6+
## This function creates a special "matrix" object that can cache its inverse.
67
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)
817
}
918

1019

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.
1322
cacheSolve <- function(x, ...) {
1423
## 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
1534
}

0 commit comments

Comments
 (0)