Skip to content

Commit 488313f

Browse files
committed
Week 3 assignment.
1 parent 7f657dd commit 488313f

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

cachematrix.R

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
7-
8-
}
9-
10-
11-
## Write a short comment describing this function
12-
13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
15-
}
1+
## makeCacheMatrix creates a function that stores a matrix for use as an argument
2+
## to cacheSolve cacheSolve finds the inverse of a matrix created with
3+
## makeCacheMatrix
4+
5+
## makeCacheMatrix returns a function with a reference to the matrix passed as an
6+
## argument The return value of this function will be used as an argument to
7+
## cacheSolve
8+
makeCacheMatrix <- function(x = matrix()) {
9+
m <- NULL # Set return value to NULL
10+
# Functions to get and set the matrix
11+
set <- function(y) {
12+
x <<- y
13+
m <<- NULL
14+
}
15+
get <- function() x
16+
setinv = function(inverse) m <<- inverse
17+
getinv = function() m
18+
list(set = set, get = get, setinv = setinv, getinv = getinv)
19+
}
20+
21+
## cacheSolve finds the inverse of a matrix created with makeCacheMatrix If the
22+
## inverse has already been found it is returned from a cache Else it is
23+
## calculated and then returned.
24+
cacheSolve <- function(x, ...) {
25+
m = x$getinv()
26+
# If not null we already calculated the inverse so return it
27+
if (!is.null(m)) {
28+
return(m)
29+
}
30+
# m was not null so find inverse
31+
data <- x$get()
32+
m <- solve(data, ...)
33+
x$setinv(m)
34+
return(m)
35+
}

0 commit comments

Comments
 (0)