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

Skip to content

Commit ddfbd2d

Browse files
author
Jesse Berman
committed
Implement makeCacheMatrix and cacheSolve functions.
1 parent 7f657dd commit ddfbd2d

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

cachematrix.R

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
1+
# Creates a special matrix object that can cache its inverse.
62
makeCacheMatrix <- function(x = matrix()) {
7-
3+
i <- NULL
4+
set <- function(y) {
5+
x <<- y
6+
i <<- NULL
7+
}
8+
get <- function() x
9+
setinverse <- function(inverse) i <<- inverse
10+
getinverse <- function() i
11+
list(set = set, get = get,
12+
setinverse = setinverse,
13+
getinverse = getinverse)
814
}
915

10-
11-
## Write a short comment describing this function
12-
16+
# Computes the inverse of a matrix that was created
17+
# using the makeCacheMatrix function.
1318
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
19+
i <- x$getinverse()
20+
if(!is.null(i)) {
21+
message("getting cached data")
22+
return(i)
23+
}
24+
m = x$get()
25+
i <- solve(m, ...)
26+
x$setinverse(i)
27+
i
1528
}

0 commit comments

Comments
 (0)