Skip to content

Commit 309e3fa

Browse files
committed
homework
1 parent 7f657dd commit 309e3fa

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

cachematrix.R

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,42 @@
44
## Write a short comment describing this function
55

66
makeCacheMatrix <- function(x = matrix()) {
7+
inv <- NULL
78

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)
818
}
919

1020

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.
1224

1325
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
1540
}
41+
42+
# Test it out
43+
test <- makeCacheMatrix()
44+
test$set(matrix(c(1:4),2,2))
45+
cacheSolve(test)

0 commit comments

Comments
 (0)