File tree Expand file tree Collapse file tree 1 file changed +26
-6
lines changed Expand file tree Collapse file tree 1 file changed +26
-6
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
1
+ # # This is a pair of functions that cache the inverse of a matrix.
3
2
4
- # # Write a short comment describing this function
3
+ # # makeCacheMatrix creates a special "matrix" object that can cache its inverse.
4
+ # # First you set the matrix, then you get the matrix,
5
+ # # then you set the inverse, then you get the inverse!
5
6
6
- makeCacheMatrix <- function (x = matrix ()) {
7
7
8
+ makeCacheMatrix <- function (x = matrix ()) {
9
+ m = NULL
10
+ set = function (y ) {
11
+ x <<- y
12
+ m <<- NULL
13
+ }
14
+ get = function () x
15
+ setinv = function (inverse ) m <<- inverse
16
+ getinv = function () m
17
+ list (set = set , get = get , setinv = setinv , getinv = getinv )
8
18
}
9
19
10
20
11
- # # Write a short comment describing this function
21
+ # # cacheSolve computes the inverse of the special "matrix" returned by makeCacheMatrix above.
22
+ # # If the inverse has already been calculated (and the matrix has not changed), then the
23
+ # # cachesolve should retrieve the inverse from the cache.
12
24
13
25
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
26
+ m = x $ getinv()
27
+ if (! is.null(m )){
28
+ message(" getting cached data" )
29
+ return (m )
30
+ }
31
+ mydata = x $ get()
32
+ m = solve(mydata , ... )
33
+ x $ setinv(m )
34
+ m
15
35
}
You can’t perform that action at this time.
0 commit comments