Skip to content

Commit 09a1ae1

Browse files
committed
Implemented cacheSolve and makeCacheMatrix functions
1 parent 7f657dd commit 09a1ae1

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

cachematrix.R

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Pair of functions that cache the inverse of a matrix.
32

4-
## Write a short comment describing this function
3+
## This function creates a special "matrix" object that can cache its inverse.
54

65
makeCacheMatrix <- function(x = matrix()) {
7-
6+
inverse <- NULL
7+
set <- function(m) {
8+
x <<- m
9+
inverse <<- NULL
10+
}
11+
get <- function() x
12+
setinverse <- function(new_inverse) inverse <<- new_inverse
13+
getinverse <- function() inverse
14+
list(set = set, get = get, setinverse = setinverse, getinverse = getinverse)
815
}
916

10-
11-
## Write a short comment describing this function
12-
13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
17+
## This function computes the inverse of the special "matrix" returned by makeCacheMatrix above
18+
cacheSolve <- function(x) {
19+
inverse <- x$getinverse()
20+
if(!is.null(inverse)) {
21+
message("getting cached data")
22+
return(inverse)
23+
}
24+
data <- x$get()
25+
inverse <- solve(data)
26+
x$setinverse(inverse)
27+
inverse
1528
}

0 commit comments

Comments
 (0)