Skip to content

Commit a6f1ae4

Browse files
committed
Submission for Assignment 2
1 parent 7f657dd commit a6f1ae4

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

cachematrix.R

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Chris Cowan
32

4-
## Write a short comment describing this function
3+
## Create a matrix object with a cache to store the inverse
54

65
makeCacheMatrix <- function(x = matrix()) {
7-
6+
inverse <- NULL
7+
set <- function(y) {
8+
x <<- y
9+
inverse <<- NULL
10+
}
11+
get <- function() x
12+
setinverse <- function(inv) inverse <<- inv
13+
getinverse <- function() inverse
14+
list(set=set, get=get, setinverse=setinverse, getinverse=getinverse)
815
}
916

1017

11-
## Write a short comment describing this function
18+
## Solve Cache Matrix object. Only calculates new result if the inverse is not
19+
## already cached. If calculation is performed, cache result for future calls
1220

1321
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
22+
inv <- x$getinverse()
23+
if(!is.null(inv)) {
24+
message("getting cached data")
25+
return(inv)
26+
}
27+
data <- x$get()
28+
inv <- solve(data)
29+
x$setinverse(inv)
30+
inv
1531
}

0 commit comments

Comments
 (0)