Skip to content

Commit 8300797

Browse files
committed
Implemented the makeCacheMatrix and cacheSolve functions
1 parent 7f657dd commit 8300797

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

cachematrix.R

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## These two functions can be used to cache the inverse of a matrix.
2+
## Create the matrix with the makeCacheMatrix function and then compute
3+
## the inverse with the cacheSolve function.
34

4-
## Write a short comment describing this function
5+
## This function wraps a matrix so that it can be used with the cacheSolve function.
56

67
makeCacheMatrix <- function(x = matrix()) {
7-
8+
inv <- NULL
9+
set <- function(y) {
10+
x <<- y
11+
inverse <<- NULL
12+
}
13+
get <- function() x
14+
setinverse <- function(inverse) inv <<- inverse
15+
getinverse <- function() inv
16+
list(set = set, get = get,
17+
setinverse = setinverse,
18+
getinverse = getinverse)
819
}
920

1021

11-
## Write a short comment describing this function
22+
## This function returns the inverse of the given matrix and caches the result
23+
## to speed up any future calls.
1224

1325
cacheSolve <- function(x, ...) {
1426
## Return a matrix that is the inverse of 'x'
27+
inv <- x$getinverse()
28+
if(!is.null(inv)) {
29+
message("getting cached data")
30+
return(inv)
31+
}
32+
data <- x$get()
33+
inv <- solve(data, ...)
34+
x$setmean(inv)
35+
inv
1536
}

0 commit comments

Comments
 (0)