Skip to content

Commit 1eb4341

Browse files
committed
Changes for Assignment 2
1 parent 8ca98ac commit 1eb4341

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

cachematrix.R

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,31 @@
44
## Write a short comment describing this function
55

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

1020

1121
## Write a short comment describing this function
1222

1323
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
24+
## 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)