Skip to content

Commit 4c2a8dc

Browse files
committed
Completed Assignment
1 parent 7f657dd commit 4c2a8dc

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

cachematrix.R

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## These two functions generate a new "matrix" object and
2+
## introduce a cached inverse resultset to avoid the costly
3+
## computation of a matrix inverse.
34

4-
## Write a short comment describing this function
5+
## Creates a set of lazy getter/setter functions for a matrix with
6+
## a cacheable inverse.
57

68
makeCacheMatrix <- function(x = matrix()) {
7-
9+
i <- NULL
10+
11+
set <- function(y) {
12+
x <<- y
13+
i <<- NULL
14+
}
15+
16+
get <- function() x
17+
setinverse <- function(inverse) i <<- inverse
18+
getinverse <- function() i
19+
20+
list(set = set, get = get,
21+
setinverse = setinverse,
22+
getinverse = getinverse)
823
}
924

1025

11-
## Write a short comment describing this function
26+
## Computes the inverse of the matrix. If the inverse has already
27+
## been calculated, the cached result will be retrieved.
1228

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

0 commit comments

Comments
 (0)