Skip to content

Commit 9a3b8ba

Browse files
committed
Initial commit
Completed functions
1 parent 7f657dd commit 9a3b8ba

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

cachematrix.R

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## This file contains two functions 'makeCacheMatrix' and 'cacheSolve'
32

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
55

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

1020

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

1325
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
1537
}

0 commit comments

Comments
 (0)