Skip to content

Commit 0e09088

Browse files
committed
cache matrix inverse
1 parent 7f657dd commit 0e09088

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

cachematrix.R

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
1+
## Create an object that contains a matrix
2+
## and caches its inverse
3+
## Use:
4+
## to create object
5+
## cmatrix <- makeCacheMatrix(matrix)
6+
## to invert
7+
## inverse <- cacheSolve(cmatrix)
58

9+
## Create an object that is a matrix caching its inverse
610
makeCacheMatrix <- function(x = matrix()) {
7-
11+
inv <- NULL
12+
set <- function(y) {
13+
x <<- y
14+
inv <<- NULL
15+
}
16+
get <- function() x
17+
setinv <- function(newinv) inv <<- newinv
18+
getinv <- function() inv
19+
20+
list(set = set, get = get, setinv = setinv, getinv = getinv)
821
}
922

1023

11-
## Write a short comment describing this function
12-
24+
## Return a matrix that is the inverse of 'x'
1325
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
26+
inv <- x$getinv()
27+
if(!is.null(inv)) {
28+
inv
29+
} else {
30+
data <- x$get()
31+
inv = solve(data, ...)
32+
x$setinv(inv)
33+
inv
34+
}
1535
}

0 commit comments

Comments
 (0)