Skip to content

Commit e7b8760

Browse files
committed
Solution of assignment 2
1 parent 7f657dd commit e7b8760

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

cachematrix.R

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
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+
## Creates a special matrix which can be used to cache the inverse of this matrix
65
makeCacheMatrix <- function(x = matrix()) {
7-
6+
i <- NULL
7+
set <- function(y) {
8+
#When setting a new matrix, the inverse needs to be cleared as well
9+
x <<- y
10+
i <<- NULL
11+
}
12+
13+
get <- function() x
14+
setInverse <- function(inverse) i <<- inverse
15+
getInverse <- function() i
16+
17+
list(set = set,
18+
get=get,
19+
getInverse=getInverse,
20+
setInverse=setInverse)
821
}
922

10-
11-
## Write a short comment describing this function
12-
23+
## Returns a matrix that is the inverse of x. The result is cached so subsequent calls to this function
24+
## returns the same result without computing it again.
1325
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
26+
i <- x$getInverse()
27+
28+
if(is.null(i)) {
29+
#We need to do the computation
30+
i <- solve(x$get())
31+
x$setInverse(i)
32+
}
33+
34+
i
1535
}

0 commit comments

Comments
 (0)