Skip to content

Commit 8a2cc60

Browse files
committed
Adds solution
1 parent 7f657dd commit 8a2cc60

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

cachematrix.R

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
1+
# These functions allows a creation of a caching matrix that
2+
# can calculate and store it's inverse
53

4+
# Constructor, makes a matrix that can cache it's inverse
65
makeCacheMatrix <- function(x = matrix()) {
7-
6+
inverse <- NULL
7+
set <- function(y) {
8+
x <<- y
9+
inverse <<- NULL
10+
}
11+
get <- function(){ x }
12+
setinverse <- function(new_inverse){
13+
inverse <<- new_inverse
14+
}
15+
getinverse <- function(){ inverse }
16+
list(set = set, get = get,
17+
setinverse = setinverse,
18+
getinverse = getinverse
19+
)
820
}
921

1022

11-
## Write a short comment describing this function
12-
23+
# Computes the inverse of a CacheMatrix, and memoizes the result
1324
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
15-
}
25+
inverse <- x$getinverse()
26+
if(!is.null(inverse)){
27+
return(inverse)
28+
}
29+
data <- x$get()
30+
inverse <- solve(data, ...)
31+
x$setinverse(inverse)
32+
inverse
33+
}

0 commit comments

Comments
 (0)