Skip to content

Commit 400f150

Browse files
committed
Week 3 assignment (author update)
1 parent 7f657dd commit 400f150

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

cachematrix.R

Lines changed: 26 additions & 8 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
3-
4-
## Write a short comment describing this function
1+
## The idea is to create a cache of the inverse of a matrix so as to not have
2+
## to compute it over and over when the matrix has not changed.
53

4+
## Function that creates a "matrix" that contains its own inverse
65
makeCacheMatrix <- function(x = matrix()) {
7-
6+
inv <- NULL
7+
set <- function(y) { # set the value of the matrix
8+
x <<- y
9+
inv <<- NULL
10+
}
11+
get <- function() x # get the value of the matrix
12+
setinverse <- function(inverse) inv <<- inverse # set the inverse
13+
getinverse <- function() inv # get the inv
14+
list(set = set, get = get,
15+
setinverse = setinverse,
16+
getinverse = getinverse)
817
}
918

1019

11-
## Write a short comment describing this function
12-
20+
## Inverts the given matrix, returning the cached inverse if it exists
1321
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
22+
inv <- x$getinverse()
23+
if(!is.null(inv)) {
24+
message("getting cached data")
25+
return(inv)
26+
}
27+
data <- x$get()
28+
inv <- solve(data, ...)
29+
x$setinverse(inv)
30+
inv
1531
}
32+
33+

0 commit comments

Comments
 (0)