Skip to content

Commit 6a7035e

Browse files
author
Daniel Schrimpf
committed
Implementation of the makeCacheMatrix and cacheSolve functions.
1 parent 9b22d21 commit 6a7035e

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

cachematrix.R

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## The function gives the posibility to create a cached matrix (only for the inverse matrix) - makeCacheMatrix
2+
## the second method returns the inverse matrix - cacheSolve
33

4-
## Write a short comment describing this function
4+
## The function creates a cached matrix with a set of functions (get, set, getInverse, setInverse)
5+
## To set the inverse matrix please call the cacheSolve function
56

67
makeCacheMatrix <- function(x = matrix()) {
7-
8+
m <- NULL
9+
set <- function(y) {
10+
x <<- y
11+
m <<- NULL
12+
}
13+
get <- function() x
14+
setInverse <- function(inverseMatrix) m <<- inverseMatrix
15+
getInverse <- function() m
16+
list(set = set, get = get,
17+
setInverse = setInverse,
18+
getInverse = getInverse)
819
}
920

1021

11-
## Write a short comment describing this function
22+
## This function takes a cacheMatrix "object". If the object has already an calculated inverse matrix
23+
## it returns the inverse matrix. Otherwise it performe the calculation, cache the result and returns the inverse matrix.
1224

1325
cacheSolve <- function(x, ...) {
1426
## Return a matrix that is the inverse of 'x'
27+
m <- x$getInverse()
28+
if(!is.null(m)) {
29+
message("getting cached data")
30+
return(m)
31+
}
32+
data <- x$get()
33+
m <- solve(data, ...)
34+
x$setInverse(m)
35+
m
1536
}

0 commit comments

Comments
 (0)