File tree Expand file tree Collapse file tree 1 file changed +32
-2
lines changed Expand file tree Collapse file tree 1 file changed +32
-2
lines changed Original file line number Diff line number Diff line change 4
4
# # Write a short comment describing this function
5
5
6
6
makeCacheMatrix <- function (x = matrix ()) {
7
+ inv <- NULL
7
8
9
+ set <- function (y ){
10
+ x <<- y
11
+ inv <<- NULL
12
+ }
13
+
14
+ get <- function () x
15
+ setinv <- function (solve ) inv <<- solve
16
+ getinv <- function () inv
17
+ list (set = set , get = get , setinv = setinv , getinv = getinv )
8
18
}
9
19
10
20
11
- # # Write a short comment describing this function
21
+ # This function delivers the inverted matrix from makeCacheMatrix,
22
+ # first looking for a cached version and if not there
23
+ # computing and returning the inversion.
12
24
13
25
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
26
+ # Return an inverted matrix of the passed matrix.
27
+ inv <- x $ getinv()
28
+
29
+ # Look for a cached version
30
+ if (! is.null(inv )) {
31
+ message(" Aw yiss, a cached version." )
32
+ return (inv )
33
+ }
34
+
35
+ # Otherwise invert the matrix.
36
+ data <- x $ get()
37
+ inv <- solve(data , ... )
38
+ x $ setinv(inv )
39
+ inv
15
40
}
41
+
42
+ # Test it out
43
+ test <- makeCacheMatrix()
44
+ test $ set(matrix (c(1 : 4 ),2 ,2 ))
45
+ cacheSolve(test )
You can’t perform that action at this time.
0 commit comments