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