File tree Expand file tree Collapse file tree 1 file changed +28
-6
lines changed Expand file tree Collapse file tree 1 file changed +28
-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 file contains two functions 'makeCacheMatrix' and 'cacheSolve'
3
2
4
- # # Write a short comment describing this function
3
+ # # makeCacheMatrix takes an ordinary matrix and extends it to allow
4
+ # # the inverse to be stored in a cache
5
5
6
6
makeCacheMatrix <- function (x = matrix ()) {
7
-
7
+ i <- NULL
8
+ set <- function (y ) {
9
+ x <<- y
10
+ i <<- NULL
11
+ }
12
+ get <- function () x
13
+ setinv <- function (inv ) i <<- inv
14
+ getinv <- function () i
15
+ list (set = set , get = get ,
16
+ setinv = setinv ,
17
+ getinv = getinv )
8
18
}
9
19
10
20
11
- # # Write a short comment describing this function
21
+ # # cacheSolve retrns the inverse of a matrix
22
+ # # It returns a cached value if one is stored, if not it:
23
+ # # calculates the inverse, stores it in the cache, and then returns it.
12
24
13
25
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
26
+ # # Return a matrix that is the inverse of 'x'
27
+ i <- x $ getinv()
28
+ if (! is.null(i )) {
29
+ message(" getting cached data" )
30
+
31
+ return (i )
32
+ }
33
+ data <- x $ get()
34
+ i <- solve(data , ... )
35
+ x $ setinv(i )
36
+ i
15
37
}
You can’t perform that action at this time.
0 commit comments