Skip to content

Commit fc35b64

Browse files
committed
caching the inverse of a matrix
1 parent 7f657dd commit fc35b64

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

cachematrix.R

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Caching the inverse matrix computation result
32

4-
## Write a short comment describing this function
3+
## wraps matrix with an object for caching the result
54

65
makeCacheMatrix <- function(x = matrix()) {
7-
6+
m <- NULL
7+
set <- function(y) {
8+
x <<- y
9+
m <<- NULL
10+
}
11+
get <- function() x
12+
setinverse <- function(inverse) m <<- inverse
13+
getinverse <- function() m
14+
list(set = set, get = get,
15+
setinverse = setinverse,
16+
getinverse = getinverse)
817
}
918

1019

11-
## Write a short comment describing this function
20+
## gets inverse matrix for wrapped matrix
1221

1322
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
23+
m <- x$getinverse()
24+
if(!is.null(m)) {
25+
message("getting cached data")
26+
return(m)
27+
}
28+
data <- x$get()
29+
m <- solve(data, ...)
30+
x$setinverse(m)
31+
m
1532
}

0 commit comments

Comments
 (0)