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