Skip to content

Commit 7f6194d

Browse files
committed
submission for grading
1 parent 7f657dd commit 7f6194d

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

cachematrix.R

Lines changed: 32 additions & 5 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
1+
## These methods can be used to cache the inverse calculation.
2+
## to create a cachable matrix, do this:
3+
## cachedMatrix <- makeCacheMatrix(matrix(1:4,2,2))
4+
## to calculate the inverse do this:
5+
## cacheSolve(cachedMatrix)
36

4-
## Write a short comment describing this function
57

8+
## This function generates an Object that holds a matrix and it's
9+
## possibily cached inverse. In order to calculate the inverse using caching
10+
## use the cacheSolve method.
611
makeCacheMatrix <- function(x = matrix()) {
12+
inverse <- NULL
13+
set <- function(y) {
14+
x <<- y
15+
inverse <<- NULL
16+
}
17+
get <- function() x
18+
setinverse <- function(inv) inverse <<- inv
19+
getinverse <- function() inverse
20+
list(set = set, get = get,
21+
setinverse = setinverse,
22+
getinverse = getinverse)
723

824
}
925

1026

11-
## Write a short comment describing this function
27+
## This function calculates the inverse of a CacheMatrix
28+
## which was constructed my using the makeCacheMatrix function.
29+
## The First call calculates the inverse, every succeeding call
30+
## uses the cached inverse.
1231

1332
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
33+
i <- x$getinverse()
34+
if(!is.null(i)) {
35+
message("getting cached data")
36+
return(i)
37+
}
38+
data <- x$get()
39+
i <- solve(data, ...)
40+
x$setinverse(i)
41+
i
1542
}

0 commit comments

Comments
 (0)