Skip to content

Commit 0c2789b

Browse files
committed
My solution!
1 parent 7f657dd commit 0c2789b

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

cachematrix.R

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Invert a matrix and cache the result, to demonstrate the power of
2+
## environments in R!
33

4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
4+
## Construct a special "matrix" object that can cache its own inverse.
75

6+
makeCacheMatrix <- function (x = matrix()) {
7+
inverse <- NULL
8+
list(get = function () x,
9+
getinverse = function () inverse,
10+
setinverse = function (inverse) inverse <<- inverse)
811
}
912

1013

11-
## Write a short comment describing this function
14+
## Compute the inverse of a special "matrix" returned by
15+
## makeCacheMatrix(), above. Only calculate the result once -- stow the
16+
## result for next time and just return the cached value, if available.
1217

13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
18+
cacheSolve <- function (x) {
19+
inverse <- x$getinverse()
20+
if (is.null(inverse)) {
21+
x$setinverse(solve(x$get()))
22+
} else {
23+
inverse
24+
}
1525
}

0 commit comments

Comments
 (0)