|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## cacheSolve and makeCacheMatrix are ment to speed up matrix calculation. |
| 2 | +## If the inverse of a matrix is allready calculated, it returns the cached value insted recalculating it. |
3 | 3 |
|
4 |
| -## Write a short comment describing this function |
| 4 | +## makeCacheMatrix creates a special matrix, which can store (set) and return (get) a matrix, but alos store (setinverse) |
| 5 | +## return (getinverse) its inverse matrix. |
| 6 | +## |
| 7 | +## makeCacheMatrix expects a matrix as argument. |
5 | 8 |
|
6 | 9 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 10 | + ## Initialize inverse as empty |
| 11 | + i <- NULL |
| 12 | + |
| 13 | + ## set function takes a new matrix and delets the inverse |
| 14 | + set <- function(y) { |
| 15 | + x <<- y |
| 16 | + i <<- NULL |
| 17 | + } |
| 18 | + |
| 19 | + ## get function returns the matrix itself |
| 20 | + get <- function() x |
| 21 | + |
| 22 | + ## setinverse sets the value of the inverse matrix |
| 23 | + setinverse <- function(inverse) i <<- inverse |
| 24 | + |
| 25 | + ## getinverse returns the value of the inverse matrix |
| 26 | + getinverse <- function() i |
| 27 | + list(set = set, get = get, |
| 28 | + setinverse = setinverse, |
| 29 | + getinverse = getinverse) |
| 30 | + |
8 | 31 | }
|
9 | 32 |
|
10 | 33 |
|
11 |
| -## Write a short comment describing this function |
12 | 34 |
|
13 |
| -cacheSolve <- function(x, ...) { |
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 35 | + |
| 36 | +## cacheSolve alows to calculate the inverse of matrix faster by utilizingt a CacheMatrix (see makeCacheMatrix). |
| 37 | +## If the inverse of a matrix is allready calculated, it returns the cached value insted recalculating it. |
| 38 | +## |
| 39 | +## cacheSolve expects a matrix created by makeCacheMatrix as argument. |
| 40 | + |
| 41 | +cacheSolve <- function(x) { |
| 42 | + ## Gets the saved inverse matrix |
| 43 | + i <- x$getinverse() |
| 44 | + |
| 45 | + ## If the inverse is not NULL (which means: allready calculated) the value is returned |
| 46 | + if(!is.null(i)) { |
| 47 | + message("getting cached data") |
| 48 | + return(i) |
| 49 | + } |
| 50 | + |
| 51 | + ## Else: Get the matrix and calculate the inverse |
| 52 | + data <- x$get() |
| 53 | + i <- solve(data) |
| 54 | + |
| 55 | + ## Save the calculated value to cache an return the value |
| 56 | + x$setinverse(i) |
| 57 | + i |
15 | 58 | }
|
0 commit comments