Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions cachematrix.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
## Put comments here that give an overall description of what your
## functions do

## Write a short comment describing this function
## The makeCacheMatrix function creates a special list object that stores a matrix and also contains a cache for its inverse. The structure is based on the example from the exercise in which the mean value of a vector was temporarily stored. This principle has been applied to matrices here.

makeCacheMatrix <- function(x = matrix()) {

inv <- NULL # Cache für die Inverse

set <- function(y) {
x <<- y # neue Matrix setzen
inv <<- NULL # Cache resetten, weil Matrix sich geändert hat
}

get <- function() {
x
}

setinv <- function(inverse) {
inv <<- inverse
}

getinv <- function() {
inv
}

list(
set = set,
get = get,
setinv = setinv,
getinv = getinv
)
}


## Write a short comment describing this function

## The cacheSolve function computes the inverse of the matrix stored inside a makeCacheMatrix object. It first checks whether the inverse is already available in the cache. If so, it returns the cached value immediately to avoid redundant computation.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getinv()

if (!is.null(inv)) {
message("getting cached data")
return(inv)
}

mat <- x$get()
inv <- solve(mat, ...) # Inverse berechnen
x$setinv(inv)
inv
}