Skip to content

Commit 288d939

Browse files
committed
First working solution.
1 parent 7f657dd commit 288d939

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

cachematrix.R

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## makeCacheMatrix() creates a special "matrix" object that
2+
## can cache its inverse.
3+
## cacheSolve() returns the inverse of such a special "matrix".
34

4-
## Write a short comment describing this function
55

6+
## Creates a "matrix" that can cache its own inverse.
67
makeCacheMatrix <- function(x = matrix()) {
7-
8+
inv <- NULL
9+
set <- function( y ) {
10+
x <<- y
11+
inv <<- NULL
12+
}
13+
get <- function() x
14+
setinv <- function( invv ) inv <<- invv
15+
getinv <- function() inv
16+
list( set = set, get = get,
17+
setinv = setinv,
18+
getinv = getinv )
819
}
920

1021

11-
## Write a short comment describing this function
12-
22+
## Returns the inverse of the special matrix 'x', either from
23+
## cache (if available), or by computing it.
24+
## ASSUME that the matrix supplied is always invertible.
1325
cacheSolve <- function(x, ...) {
1426
## Return a matrix that is the inverse of 'x'
27+
inv <- x$getinv()
28+
if ( !is.null(inv) ) {
29+
message("Getting cached data...")
30+
return( inv )
31+
}
32+
data <- x$get()
33+
inv <- solve( data )
34+
x$setinv( inv )
35+
inv
1536
}

0 commit comments

Comments
 (0)