Skip to content

Commit 84f801d

Browse files
committed
Update cachematrix.R
1 parent 7f657dd commit 84f801d

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

cachematrix.R

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,33 @@
33

44
## Write a short comment describing this function
55

6-
makeCacheMatrix <- function(x = matrix()) {
7-
6+
makeCacheMatrix <- function(x = matrix())
7+
8+
{
9+
## Check if first run
10+
if (!exists("cachedMatrix")){
11+
cachedMatrix <<- x
12+
cachedInvMatrix <<- solve(x)
13+
cachedInvMatrix
14+
}else if (identical(x, cachedMatrix) ){
15+
## Check if we have a cached matrix
16+
cachedInvMatrix
17+
}else{
18+
## Save the original matrix
19+
cachedMatrix <<- x
20+
## Save the identity matrix
21+
cachedInvMatrix <<- solve(x)
22+
23+
}
24+
## return the cached inverse matrix
25+
cachedInvMatrix
26+
827
}
928

1029

1130
## Write a short comment describing this function
1231

13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
32+
cacheSolve <- function(x, ...){
33+
## Return a matrix that is the inverse of 'x'
34+
makeCacheMatrix(x)
1535
}

0 commit comments

Comments
 (0)