Skip to content

Commit 7984f39

Browse files
author
Nada O'Neal
committed
Added makeCacheMatrix and cacheSolve functions
1 parent 7f657dd commit 7984f39

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

cachematrix.R

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
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+
## At last, you can cache your inverted matrices, like a squirrel hiding delicious numerical acorns!
2+
## * Use makeCacheMatrix(matrix) to create a cache object for your matrix
3+
## * Pass that object to cacheSolve(cacheMatrix) to check the cache for the inverse, if it exists,
4+
## or calculate the inverse and cache it away for the future
55

6+
## makeCacheMatrix takes your numerical matrix and then returns a special caching matrix with functions to:
7+
# - set the value of your matrix
8+
# - get the value of your matrix
9+
# - set the inverse of your matrix
10+
# - get the inverse of your matrix
11+
## This function does not actually calculate the inverse of your matrix
612
makeCacheMatrix <- function(x = matrix()) {
7-
13+
m <- NULL
14+
set <- function(y) {
15+
x <<- y
16+
m <<- NULL
17+
}
18+
get <- function() x
19+
setinverse <- function(inverseMatrix) m <<- inverseMatrix
20+
getinverse <- function() m
21+
list(set = set, get = get,
22+
setinverse = setinverse,
23+
getinverse = getinverse)
824
}
925

1026

11-
## Write a short comment describing this function
12-
27+
## cacheSolve takes your cacheMatrix, created above, and will check to see if the inverse is already cached
28+
## - if it's cached, cacheSolve warns "getting cached data" and returns the cache
29+
## - if it's not cached, cacheSolve calculates the inverse of your original matrix, caches it,
30+
## and returns the value
1331
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
32+
m <- x$getinverse()
33+
if(!is.null(m)) {
34+
message("getting cached data")
35+
return(m)
36+
}
37+
data <- x$get()
38+
m <-solve(data, ...)
39+
x$setinverse(m)
40+
m
1541
}
42+

0 commit comments

Comments
 (0)