Skip to content

Commit 00c6482

Browse files
author
Sebastian Stabinger
committed
Adds the commented solution of the assignment
I.e. cached version of solving/inverting a matrix
1 parent 7f657dd commit 00c6482

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

cachematrix.R

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

4-
## Write a short comment describing this function
5-
4+
## This function returns a list of functions used to manage the matrix
5+
## content and the memorized inverse of the matrix
66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
m <- NULL # Contains the memorized inverse matrix
8+
set <- function(y){ # Reassign matrix
9+
x <<- y
10+
m <<- NULL # Memorized value is not valid anymore
11+
}
12+
get <- function() x # Return matrix content
13+
setinv <- function(inv) m <<- inv # Set a new memorized value
14+
getinv <- function() m # Return memorized inverse
15+
list(set = set,
16+
get = get,
17+
setinv = setinv,
18+
getinv = getinv) # Provides the interface for the functions
819
}
920

10-
11-
## Write a short comment describing this function
12-
21+
## Returns the inverse of the matrix x (created with makeCacheMatrix).
22+
## If the inverse has already been calculated before for the same
23+
## matrix, the computation will not be repeated, but a memorized
24+
## version of the inverse will be returned
1325
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
26+
m <- x$getinv() # Get currently memorized value
27+
## If a memorized value exists, return it
28+
if(!is.null(m)){
29+
message("returning memorized data")
30+
return(m)
31+
}
32+
## If no memorized value exists
33+
data <- x$get() # Get current matrix represented by x
34+
m <- solve(data, ...) # Get the inverse
35+
x$setinv(m) # Memorize this inverse
36+
m # Return the inverse
1537
}

0 commit comments

Comments
 (0)