Skip to content

Commit 9d54044

Browse files
committed
Complete solution
1 parent 7f657dd commit 9d54044

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

cachematrix.R

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Caching the Inverse of a Matrix
32

4-
## Write a short comment describing this function
3+
## makeCacheMatrix creates a special object that stores
4+
## a matrix and its inverse.
55

66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
i <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
i <<- NULL
11+
}
12+
get <- function() x
13+
setinverse <- function(inverse) i <<- inverse
14+
getinverse <- function() i
15+
list(set = set, get = get,
16+
setinverse = setinverse,
17+
getinverse = getinverse)
818
}
919

1020

11-
## Write a short comment describing this function
21+
## cacheSolve computes the inverse of the given matrix
22+
## (created with makeCacheMatrix) and caches the result.
1223

1324
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
25+
i <- x$getinverse()
26+
if(is.null(i)) { # No cached inverse
27+
m <- x$get() # Get the original matrix,
28+
i <- solve(m, ...) # find the inverse,
29+
x$setinverse(i) # cache it.
30+
} else {
31+
message("Getting cached data")
32+
}
33+
i
1534
}

0 commit comments

Comments
 (0)