File tree Expand file tree Collapse file tree 1 file changed +27
-3
lines changed Expand file tree Collapse file tree 1 file changed +27
-3
lines changed Original file line number Diff line number Diff line change 1- # # Put comments here that give an overall description of what your
2- # # functions do
1+ # # makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse
32
4- # # Write a short comment describing this function
3+ # # cacheSolve: This function computes the inverse of the special "matrix" returned by makeCacheMatrix above.
4+ # # If the inverse has already been calculated (and the matrix has not changed), then the cachesolve should retrieve
5+ # # the inverse from the cache
6+
7+ # # Computing the inverse of a square matrix can be done with the solve function in R.
8+ # # For example, if X is a square invertible matrix, then solve(X) returns its inverse.
59
610makeCacheMatrix <- function (x = matrix ()) {
11+ I <- NULL
12+ set <- function (y ) {
13+ x <<- y
14+ I <<- NULL
15+ }
16+ get <- function () x
17+ setinverse <- function (solve ) I <<- solve
18+ getinverse <- function () I
19+ list (set = set , get = get ,
20+ setinverse = setinverse ,
21+ getinverse = getinverse )
722
823}
924
@@ -12,4 +27,13 @@ makeCacheMatrix <- function(x = matrix()) {
1227
1328cacheSolve <- function (x , ... ) {
1429 # # Return a matrix that is the inverse of 'x'
30+ I <- x $ getinverse()
31+ if (! is.null(I )) {
32+ message(" getting cached data" )
33+ return (I )
34+ }
35+ data <- x $ get()
36+ I <- solve(data , ... )
37+ x $ setinverse(I )
38+ I
1539}
You can’t perform that action at this time.
0 commit comments