Skip to content

Commit 275443b

Browse files
committed
Creates a function that caches the inverse of a matrix
Solves programming assignment 2 by creating a pair of functions that work together to calculate the inverse of a matrix and cache the result for future use.
1 parent 7f657dd commit 275443b

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

cachematrix.R

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## A pair of functions that calculate the inverse of a matix
2+
## and cache the result.
33

4-
## Write a short comment describing this function
4+
## Creates a "matrix" object that can cache it's inverse
55

66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
i <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
i <<- NULL
11+
}
12+
get <- function() x
13+
setinverse <- function(inverse) i <<- inverse
14+
getinverse <- function() i
15+
list(set = set, get = get,
16+
setinverse = setinverse,
17+
getinverse = getinverse)
818
}
919

1020

11-
## Write a short comment describing this function
21+
## Computes inverse of "matrix" object and caches result
1222

1323
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
24+
i <- x$getinverse()
25+
if(!is.null(i)) {
26+
message("getting cached data")
27+
return(i)
28+
}
29+
data <- x$get()
30+
i <- solve(data, ...)
31+
x$setinverse(i)
32+
i
1533
}

0 commit comments

Comments
 (0)