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

Skip to content

Commit c179770

Browse files
author
rubyist
committed
caching matrix
1 parent 7f657dd commit c179770

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

cachematrix.R

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## These functions provide a method of caching matrix inversions
32

4-
## Write a short comment describing this function
3+
## makeCacheMatrix creates an object wrapping a matrix that allows the calculated
4+
## inverse to be cached.
55

66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
i <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
i <<- NULL
11+
}
12+
13+
get <- function() x
14+
setsolve <- function(inv) i <<- inv
15+
getsolve <- function() i
16+
list(set = set, get = get, setsolve = setsolve, getsolve = getsolve)
817
}
918

1019

11-
## Write a short comment describing this function
20+
## cacheSolve will invert a matrix created by makeCacheMatrix and cache the results,
21+
## returning a matrix that is the inverse of 'x'.
1222

1323
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
24+
i <- x$getsolve()
25+
if (!is.null(i)) {
26+
return(i)
27+
}
28+
m <- x$get()
29+
i <- solve(m, ...)
30+
x$setsolve(i)
31+
i
1532
}

0 commit comments

Comments
 (0)