Skip to content

Commit 0b01974

Browse files
committed
My solution to the programming assignment
1 parent 7f657dd commit 0b01974

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

cachematrix.R

Lines changed: 26 additions & 9 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
3-
4-
## Write a short comment describing this function
1+
## These functions calculate and cache the inverse of a matrix
52

3+
## This function creates a special "matrix" object that can cache its inverse.
64
makeCacheMatrix <- function(x = matrix()) {
7-
5+
inverse <- NULL
6+
set <- function(y) {
7+
x <<- y
8+
inverse <<- NULL
9+
}
10+
get <- function() x
11+
setinverse <- function(i) inverse <<- i
12+
getinverse <- function() inverse
13+
list(set = set, get = get,
14+
getinverse = getinverse,
15+
setinverse = setinverse)
816
}
917

10-
11-
## Write a short comment describing this function
12-
18+
## This function computes the inverse of the special "matrix" returned by
19+
## `makeCacheMatrix` above. If the inverse has already been calculated
20+
## (and the matrix has not changed), then `cacheSolve` should retrieve the
21+
## inverse from the cache.
1322
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
23+
inverse <- x$getinverse()
24+
if (!is.null(inverse)) {
25+
message("getting cached data")
26+
return(inverse)
27+
}
28+
data <- x$get()
29+
inverse <- solve(data, ...)
30+
x$setinverse(inverse)
31+
inverse
1532
}

0 commit comments

Comments
 (0)