Skip to content

Commit d41d80c

Browse files
committed
Update cachematrix.R
effort rdpeng#1
1 parent 7f657dd commit d41d80c

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

cachematrix.R

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,43 @@
22
## functions do
33

44
## Write a short comment describing this function
5+
##crete a with makeCacheMatrix framework
6+
## > a<- makeCacheMatrix()
7+
##create Matrix
8+
## > a$set(1:4, 2)
9+
##inspect Matrix
10+
## > a$get()
11+
## run cachesolve(a) to see if the results are cached
12+
## > cachesolve(a)
13+
## Run again to see cached results
14+
## > cachesolve(a)
515

616
makeCacheMatrix <- function(x = matrix()) {
7-
17+
m <- NULL
18+
set <- function(y ,z) {
19+
x <<- matrix(y, nrow=z)
20+
m <<- NULL
21+
}
22+
get <- function() x
23+
setmatrix <- function(solve) m <<- solve
24+
getmatrix <- function() m
25+
list(set = set, get = get,
26+
setmatrix = setmatrix,
27+
getmatrix = getmatrix)
828
}
929

1030

1131
## Write a short comment describing this function
1232

1333
cacheSolve <- function(x, ...) {
1434
## Return a matrix that is the inverse of 'x'
35+
m <- x$getmatrix()
36+
if(!is.null(m)) {
37+
message("getting cached data")
38+
return(m)
39+
}
40+
data <- x$get()
41+
m <- solve(data, ...)
42+
x$setmatrix(m)
43+
m
1544
}

0 commit comments

Comments
 (0)