Skip to content

Commit 34216a7

Browse files
committed
completed assignment
1 parent 7f657dd commit 34216a7

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

cachematrix.R

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## R function that is able to cache potentially time-consuming matrix inverse computation.
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+
i <- NULL
7+
set <- function(y) {
8+
x <<- y
9+
i <<- NULL
10+
}
11+
get <- function() x
12+
setinverse <- function(solve) i <<- solve
13+
getinverse <- function() i
14+
list(set = set, get = get,
15+
setinverse = setinverse,
16+
getinverse = getinverse)
817
}
918

1019

11-
## Write a short comment describing this function
20+
## This function computes the inverse of the special "matrix" returned by
21+
## makeCacheMatrix above. If the inverse has already been calculated
22+
## (and the matrix has not changed), then the cachesolve should retrieve
23+
## the inverse from the cache.
1224

1325
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
26+
## Return a matrix that is the inverse of 'x'
27+
i <- x$getinverse()
28+
if(!is.null(i)) {
29+
message("getting cached data")
30+
return(i)
31+
}
32+
data <- x$get()
33+
i <- solve(data, ...)
34+
x$setinverse(i)
35+
i
1536
}

0 commit comments

Comments
 (0)