Skip to content

Commit 4f5e5c2

Browse files
author
Oliver Ochs
committed
Did my homework
1 parent 7f657dd commit 4f5e5c2

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

cachematrix.R

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
1+
## This function creates a special "matrix" object that can cache its inverse
2+
## This special matrix has an internal cache "i" to do this.
3+
## There are three "methods" in this matrix:
4+
## set(value) to set the value
5+
## get() to retrive the value
6+
## setinverse(value) to fill the cache
7+
## getinverse() to get data from cache
68
makeCacheMatrix <- function(x = matrix()) {
7-
9+
i <- NULL
10+
set <- function(y) {
11+
x <<- y
12+
i <<- NULL
13+
}
14+
get <- function() x
15+
setinverse<- function(i) i <<-i
16+
getinverse <- function() i
17+
list(set = set, get = get,
18+
setinverse = setinverse,
19+
getinverse = getinverse)
820
}
921

10-
11-
## Write a short comment describing this function
12-
22+
## This function computes the inverse of the special "matrix"
23+
## returned by makeCacheMatrix above. If the inverse has already been
24+
## calculated (and the matrix has not changed), then cacheSolve retrieves
25+
## the inverse from the cache.
26+
## Otherwise the inverse function (solve) is used to invert the matrix.
1327
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
28+
i <- x$getinverse()
29+
if (!is.null(i)) {
30+
message("found cached inverse matrix")
31+
return(i)
32+
} else {
33+
i <- solve(x$get())
34+
x$setinverse(i)
35+
return(i)
36+
}
1537
}

0 commit comments

Comments
 (0)