Skip to content

Commit 79dab42

Browse files
committed
Programming Assignment 2 submission.
1 parent 7f657dd commit 79dab42

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

cachematrix.R

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
1+
## Functions create and cache the inverse of a matrix.
52

3+
## Creates an object to hold a matrix and its inverse
64
makeCacheMatrix <- function(x = matrix()) {
7-
5+
# Holds the inverse matrix
6+
i <- NULL
7+
8+
# Sets the original matrix and initializes the inverse to NULL
9+
set <- function(y) {
10+
x <<- y
11+
i <<- NULL
12+
}
13+
14+
# Returns the original matrix
15+
get <- function() x
16+
17+
# Sets the inverse matrix
18+
setinverse <- function(inverse) i <<- inverse
19+
20+
# Gets the inverse matrix
21+
getinverse <- function() i
22+
23+
# Returns a list holding the above functions
24+
list(set = set, get = get, setinverse = setinverse, getinverse = getinverse)
825
}
926

1027

11-
## Write a short comment describing this function
12-
28+
## Gets an inverse matrix from cache or created and caches an inverse matrix.
1329
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
30+
# Attempt to get a cached inverse from the matrix container object
31+
i <- x$getinverse()
32+
if(!is.null(i)){
33+
message("getting cached inverse")
34+
return(i)
35+
}
36+
# A cached inverse was not found, make one, cache it and return it.
37+
data <- x$get()
38+
i <- solve(data)
39+
x$setinverse(i)
40+
i
1541
}

0 commit comments

Comments
 (0)