Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 515af0b

Browse files
committed
Solution, 1st attempt
1 parent 7f657dd commit 515af0b

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

cachematrix.R

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
11
## Put comments here that give an overall description of what your
22
## functions do
33

4-
## Write a short comment describing this function
4+
## makeCacheMatrix creates a special matrix object that can cache 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

10-
11-
## Write a short comment describing this function
20+
## cacheSolve computes the inverse of the special matrix object that is returned
21+
## by makeCacheMatrix. If the inverse has already been computed on this
22+
## particular matrix object, the inverse won't be recalculated; instead, it
23+
## is retrieved from the cache.
1224

1325
cacheSolve <- function(x, ...) {
1426
## 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)