Skip to content

Commit 2337d22

Browse files
committed
implemented cachematrix
1 parent 7f657dd commit 2337d22

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

cachematrix.R

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

66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
inverse <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
inverse <<- NULL
11+
}
12+
get <- function() x
13+
setinverse <- function(inv) inverse <<- inv
14+
getinverse <- function() inverse
15+
list(set = set, get = get,
16+
setinverse = setinverse,
17+
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+
m <- x$getinverse()
26+
if(!is.null(m)) {
27+
message("getting cached data")
28+
return(m)
29+
} else {
30+
message("no cached data")
31+
}
32+
data <- x$get()
33+
m <- solve(data, ...)
34+
x$setinverse(m)
35+
m
1536
}

0 commit comments

Comments
 (0)