File tree Expand file tree Collapse file tree 1 file changed +29
-9
lines changed Expand file tree Collapse file tree 1 file changed +29
-9
lines changed Original file line number Diff line number Diff line change 1
- # # Put comments here that give an overall description of what your
2
- # # functions do
3
-
4
- # # Write a short comment describing this function
5
-
1
+ # # funtcion makeCacheMatrix() creates a special "matrix" object that can cache its inverse.
6
2
makeCacheMatrix <- function (x = matrix ()) {
7
-
3
+ m <- NULL
4
+ set <- function (y ) {
5
+ x <<- y
6
+ m <<- NULL
7
+ }
8
+ get <- function () x
9
+ setInverse <- function (solve ) m <<- solve
10
+ getInverse <- function () m
11
+ list (set = set , get = get ,
12
+ setInverse = setInverse ,
13
+ getInverse = getInverse )
8
14
}
9
15
16
+ # # cacheSolve() computes the inverse of the special "matrix" returned by makeCacheMatrix above.
17
+ # # If the inverse has already been calculated (and the matrix has not changed), then the cachesolve
18
+ # # should retrieve the inverse from the cache.
19
+ cacheSolve <- function (x , ... ) {
20
+ m <- x $ getInverse()
10
21
11
- # # Write a short comment describing this function
22
+ # # Return a matrix that is the inverse of 'x' if the inverse has already been calculated and cached.
23
+ if (! is.null(m )) {
24
+ message(" getting cached data" )
25
+ return (m )
26
+ }
12
27
13
- cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
28
+ # # Calculate the inverse of 'x' if the cached data doesn't exist.
29
+ data <- x $ get()
30
+ m <- solve(data , ... )
31
+ x $ setInverse(m )
32
+
33
+ # # Return a matrix that is the inverse of 'x'
34
+ m
15
35
}
You can’t perform that action at this time.
0 commit comments