forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
40 lines (35 loc) · 1.12 KB
/
Copy pathcachematrix.R
File metadata and controls
40 lines (35 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
## Put comments here that give an overall description of what your
## functions do
## makeCacheMatrix returns a list containing functions for getting and setting
## a matrix as well as its inverse
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
get <- function() x
set <- function(y) {
x <<- y
inv <<- NULL
}
getinverse <- function() inv
setinverse <- function(y) inv <<- y
list(set = set,
setinverse = setinverse,
get = get,
getinverse = getinverse)
}
## the cacheSolve function takes a cacheMatrix generated by the
## makeCacheMatrix function and returns the inverse
## Before solving, it will first attempt to find the cached inverse in
## cacheMatrix
cacheSolve <- function(cacheMatrix, ...) {
## Return a matrix that is the inverse of 'cacheMatrix'
m <- cacheMatrix$getinverse()
if(!is.null(m)) {
message("getting cached data")
# since we have a cached matrix, return it instead of solving
return(m)
}
data <- cacheMatrix$get() # get the original matrix
m <- solve(data, ...) # invert it
cacheMatrix$setinverse(m) # cache the inverse
m
}