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
+ # # Functions for computing matrix inverses and saving a cache
2
+ # # to remove the need for recomputation
3
3
4
- # # Write a short comment describing this function
4
+ # # makeCacheMatrix returns an object with methods for getting/setting
5
+ # # a matrix and its inverse
5
6
6
7
makeCacheMatrix <- function (x = matrix ()) {
7
-
8
+ matr <- x
9
+ inv <- NULL
10
+ get <- function () matr
11
+ set <- function (newMatr ) {
12
+ matr <<- newMatr
13
+ inv <<- NULL
14
+ }
15
+ getinv <- function () inv
16
+ setinv <- function (newInv ) {
17
+ inv <<- newInv
18
+ }
19
+ list (set = set , setinv = setinv ,
20
+ get = get , getinv = getinv )
8
21
}
9
22
10
23
11
- # # Write a short comment describing this function
24
+ # # Computes the inverse of a matrix or returns a cached value if
25
+ # # one is present
12
26
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
+ inv <- x $ getinv()
30
+ if (! is.null(inv )) {
31
+ return (inv )
32
+ } else {
33
+ inv <- solve(x $ get(), ... )
34
+ x $ setinv(inv )
35
+ return (inv )
36
+ }
15
37
}
You can’t perform that action at this time.
0 commit comments