Skip to content

Commit 226cd4e

Browse files
author
Jonas Friedemann Grote
committed
Solve (heh, a pun) assignment.
1 parent 7f657dd commit 226cd4e

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

cachematrix.R

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
11
## Put comments here that give an overall description of what your
22
## functions do
33

4-
## Write a short comment describing this function
4+
## Create a matrix that will cache its inverse.
55

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

819
}
920

1021

11-
## Write a short comment describing this function
22+
## Return a matrix that is the inverse of 'x'
23+
## Requires x to be of "makeCacheMatrix" type
1224

1325
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
26+
inverse <- x$getinverse()
27+
if (!is.null(inverse)) {
28+
return(inverse)
29+
}
30+
data <- x$get()
31+
inverse <- solve(data)
32+
x$setinverse(inverse)
33+
inverse
1534
}

0 commit comments

Comments
 (0)