Skip to content

Commit 29839fb

Browse files
committed
write functions and comments
1 parent 7f657dd commit 29839fb

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

cachematrix.R

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,34 @@
22
## functions do
33

44
## Write a short comment describing this function
5-
5+
## This function generate a list object including matrix x and cache functions with closure
66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
m <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
m <<- NULL
11+
}
12+
get <- function() x
13+
setsolve <- function(solve) m <<- solve
14+
getsolve <- function() m
15+
list(set = set, get = get,
16+
setsolve = setsolve,
17+
getsolve = getsolve)
818
}
919

1020

1121
## Write a short comment describing this function
12-
22+
## if object x has a cache, return cache data
23+
## Or calcuate inverse matrix with solve function and save to cache
1324
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
25+
## Return a matrix that is the inverse of 'x'
26+
m <- x$getsolve()
27+
if(!is.null(m)) {
28+
message("getting cached data")
29+
return(m)
30+
}
31+
data <- x$get()
32+
m <- solve(data, ...)
33+
x$setsolve(m)
34+
m
1535
}

0 commit comments

Comments
 (0)