Skip to content

Commit e44584d

Browse files
author
Matthias Georgi
committed
solution
1 parent 7f657dd commit e44584d

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

cachematrix.R

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,46 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## makeCacheMatrix and solveCache can be used to cache
2+
## the matrix inverse computation. A matrix created by
3+
## makeCacheMatrix is a
4+
## special list containing functions to set and get
5+
## the matrix and to get and set the inverse as well.
6+
## solveCache will calculate the inverse and cache
7+
## the result of that computation.
38

4-
## Write a short comment describing this function
59

10+
## Create a matrix which allows caching the inverse
11+
## Return a list with four functions
12+
## 1. set the matrix
13+
## 2. get the matrix
14+
## 3. set the inverse of the matrix
15+
## 4. get the inverse of the matrix
616
makeCacheMatrix <- function(x = matrix()) {
7-
17+
i <- NULL
18+
set <- function(y) {
19+
x <<- y
20+
i <<- NULL
21+
}
22+
get <- function() x
23+
setinverse <- function(inverse) i <<- inverse
24+
getinverse <- function() i
25+
list(set = set,
26+
get = get,
27+
setinverse = setinverse,
28+
getinverse = getinverse)
829
}
930

10-
11-
## Write a short comment describing this function
12-
31+
## cacheSolve returns the inverse of a special
32+
## matrix created with the makeCacheMatrix
33+
## results of the computation will be cached
1334
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
35+
## Return a matrix that is the inverse of 'x'
36+
37+
i <- x$getinverse()
38+
if(!is.null(i)) {
39+
message("getting cached data")
40+
return(i)
41+
}
42+
data <- x$get()
43+
i <- solve(data, ...)
44+
x$setinverse(i)
45+
i
1546
}

0 commit comments

Comments
 (0)