Skip to content

Commit a23eded

Browse files
committed
Inverted matrix caching using FP
1 parent 9b22d21 commit a23eded

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

cachematrix.R

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## This code uses caching to avoid recalculating inverse matrices.
32

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

6-
makeCacheMatrix <- function(x = matrix()) {
4+
## Function below allows to create a matrix that can then store its inverse in cache.
75

6+
makeCacheMatrix <- function(x = matrix()) {
7+
i <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
i <<- NULL
11+
}
12+
get <- function() x
13+
setinverse <- function(inverse) i <<- inverse
14+
getinverse <- function() i
15+
list(set = set, get = get,
16+
setinverse = setinverse,
17+
getinverse = getinverse)
818
}
919

1020

11-
## Write a short comment describing this function
21+
## Function below allows to get the cached inverse matrix of the object created by makeCacheMatrix
22+
## or calculate the inverse matrix if necessary.
1223

1324
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
25+
i <- x$getinverse()
26+
if(!is.null(i)) {
27+
message("getting cached data")
28+
return(i)
29+
}
30+
data <- x$get()
31+
i <- solve(data, ...)
32+
x$setinverse(i)
33+
i
1534
}

0 commit comments

Comments
 (0)