Skip to content

Commit dcb731f

Browse files
author
e-lo
committed
Attempt rdpeng#1
Hope this is it!
1 parent 7f657dd commit dcb731f

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

cachematrix.R

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## The following functions creates a special object
2+
## to be able to cache the inverse of a matrix
3+
## so it doesn't have to be calculated repeatedly from
4+
## w/in a program
35

4-
## Write a short comment describing this function
6+
## returns a list of functions to get and set the matrix
7+
## and get and set the cached matrix
58

69
makeCacheMatrix <- function(x = matrix()) {
7-
10+
c <- NULL
11+
set <- function(y) {
12+
x <<- y
13+
c <<- NULL ## this resets the cache b/c the matrix has been changed
14+
}
15+
get <- function() x
16+
setcache <- function(solve) c <<- solve
17+
getcache <- function() c
18+
list(set = set, get = get,
19+
setcache = setcache,
20+
getcache = getcache)
821
}
922

1023

11-
## Write a short comment describing this function
24+
## This function checks to see if the inverse matrix
25+
## has already been calculated and if not, it calculates
26+
## it and sets it so it doesn't have to be done again
1227

1328
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
29+
## Return a matrix that is the inverse of 'x'
30+
c <- x$getcache()
31+
if(!is.null(c)) {
32+
message("getting cached data")
33+
return(c)
34+
}
35+
matrix <- x$get()
36+
c <- solve(matrix, ...) #calculate inverse
37+
x$setcache(c)
38+
c
1539
}

0 commit comments

Comments
 (0)