10000 #1 complete the R code cachematrix.R · Minnie223/ProgrammingAssignment2@a32bfb8 · GitHub
[go: up one dir, main page]

Skip to content

Commit a32bfb8

Browse files
committed
rdpeng#1 complete the R code cachematrix.R
1 parent 7f657dd commit a32bfb8

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

cachematrix.R

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Use a pair of functions to cache the inverse of a matrix.
32

4-
## Write a short comment describing this function
53

6-
makeCacheMatrix <- function(x = matrix()) {
74

5+
## Creates a special "matrix" object that can cache its inverse
6+
7+
makeCacheMatrix <- function(x = matrix()) {
8+
m <- NULL
9+
set <- function(y) {
10+
x <<- y
11+
m <<- NULL
12+
}
13+
get <- function() x
14+
setsolve <- function(solve) m <<- solve
15+
getsolve <- function() m
16+
list(set = set, get = get,
17+
setsolve = setsolve,
18+
getsolve = getsolve)
819
}
920

1021

11-
## Write a short comment describing this function
22+
## Computes the inverse of the special "matrix" returned by makeCacheMatrix.
23+
## If the inverse has already been calculated (and the matrix has not changed), then the cachesolve should retrieve the inverse from the cache.
1224

1325
cacheSolve <- function(x, ...) {
14-
## 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
35+
1536
}

0 commit comments

Comments
 (0)
0