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