Skip to content

Commit 63b57c4

Browse files
committed
my w3 programming assignment
1 parent 7f657dd commit 63b57c4

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

cachematrix.R

Lines changed: 27 additions & 7 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+
## This function creates a special "matrix" object that can cache its inverse.
52

63
makeCacheMatrix <- function(x = matrix()) {
7-
4+
inv <- NULL
5+
set <- function(y) {
6+
x <<- y
7+
inv <<- NULL
8+
}
9+
get <- function() x
10+
setinverse <- function(inverse) inv <<- inverse
11+
getinverse <- function() inv
12+
list(set = set,
13+
get = get,
14+
setinverse = setinverse,
15+
getinverse = getinverse)
816
}
917

1018

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

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

0 commit comments

Comments
 (0)