Skip to content

Commit feebd01

Browse files
committed
initial design
1 parent 7f657dd commit feebd01

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

cachematrix.R

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,49 @@
22
## functions do
33

44
## Write a short comment describing this function
5+
makeCacheMatrix is a function that creates a special matrix that can be used to cache
6+
its inverse, since matrix inversion can be time consuming. Instead of recalculating
7+
the inverse of a matrix, it will be stored in a cache for retrieval.
58

6-
makeCacheMatrix <- function(x = matrix()) {
9+
makeCacheMatrix returns a list with a function to
10+
1. set the value of the matrix
11+
2. get the value of the matrix
12+
3. set the value of the matrix inverse
13+
4. get the value of the matrix inverse
714

15+
makeCacheMatrix <- function(x = matrix()) {
16+
m <- NULL
17+
set <- function(y) {
18+
x <<- y
19+
m <<- NULL
20+
}
21+
get <- function() x
22+
setmatrixInverse <- function(calcMatrixInverse) m <<- matrixInverse
23+
getmatrixInverse <- function() m
24+
list(set = set, get = get,
25+
setmatrixInverse = setmatrixInverse,
26+
getmatrixInverse = getmatrixInverse)
827
}
928

1029

1130
## Write a short comment describing this function
1231

32+
The following function calculates the matrix inverse of the special "vector"
33+
created with the above function. However, it first checks to see if the
34+
matrix inverse has already been calculated. If so, it `get`s the matrix inverse from the
35+
cache and skips the computation. Otherwise, it calculates the matrix inverse of
36+
the data and sets the value of the matrix inverse in the cache via the `setMatrixInverse`
37+
function.
38+
1339
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
40+
## Return a matrix that is the inverse of 'x'
41+
m <- x$matrixInverse()
42+
if(!is.null(m)) {
43+
message("getting cached data")
44+
return(m)
45+
}
46+
data <- x$get()
47+
m <- calcMatrixInverse(data, ...)
48+
x$setmatrixInverse(m)
49+
m
1550
}

0 commit comments

Comments
 (0)