Skip to content

Commit de13a33

Browse files
committed
Inverse of matrix
1 parent 7f657dd commit de13a33

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

cachematrix.R

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
11
## Put comments here that give an overall description of what your
22
## functions do
33

4-
## Write a short comment describing this function
5-
4+
## This function creates a special "matrix" object
5+
## that can cache its inverse.
66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
inv <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
inv <<- NULL
11+
}
12+
get <- function() x
13+
setinverse <- function(inverse) inv <<- inverse
14+
getinverse <- function() inv
15+
16+
list(set = set, get = get,
17+
setinverse = setinverse,
18+
getinverse = getinverse)
819
}
920

1021

11-
## Write a short comment describing this function
12-
22+
## This function computes the inverse of the special
23+
##"matrix" returned by `makeCacheMatrix` above. If the inverse has
24+
##already been calculated (and the matrix has not changed), then
25+
##this function` retrieves the inverse from the cache.
1326
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
15-
}
27+
## Return a matrix that is the inverse of 'x'
28+
inv <- x$getinverse()
29+
if(!is.null(inv)) {
30+
message("getting cached data")
31+
return(inv)
32+
}
33+
data <- x$get()
34+
inv <- solve(data, ...)
35+
x$setinverse(inv)
36+
inv
37+
}

0 commit comments

Comments
 (0)