Skip to content

Commit 552118b

Browse files
committed
cacheable matrix solver ;)
1 parent e4eed41 commit 552118b

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

cachematrix.R

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
1+
## Functions for cache result of matrix inversion.
2+
##
3+
## Example:
4+
## > x <- makeCacheMatrix(matrix(1:4, nrow = 2, ncol=2))
5+
## > cacheSolve(x)
6+
## [,1] [,2]
7+
## [1,] -2 1.5
8+
## [2,] 1 -0.5
9+
## > cacheSolve(x)
10+
## getting cached data
11+
## [,1] [,2]
12+
## [1,] -2 1.5
13+
## [2,] 1 -0.5
514

15+
## Make special cachable version of matrix
616
makeCacheMatrix <- function(x = matrix()) {
7-
17+
c <- NULL
18+
set <- function(y) {
19+
x <<- y
20+
c <<- NULL
21+
}
22+
get <- function() x
23+
setsolve <- function(solved) c <<- solved
24+
getsolve <- function() c
25+
list(set = set, get = get,
26+
setsolve = setsolve,
27+
getsolve = getsolve)
828
}
929

1030

11-
## Write a short comment describing this function
12-
31+
## Solve matrix or get cached result if already solved
1332
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
33+
m <- x$getsolve()
34+
if(!is.null(m)) {
35+
message("getting cached data")
36+
return(m)
37+
}
38+
data <- x$get()
39+
m <- solve(data, ...)
40+
x$setsolve(m)
41+
m
1542
}

0 commit comments

Comments
 (0)