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 (36 loc) · 1.41 KB
/
Copy pathcachematrix.R
File metadata and controls
40 lines (36 loc) · 1.41 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
## The functions makeCacheMatrix and cachesolve will store a matrix and return its
## inverse by calculating it and storing it once rather than calculating it
## everytime cachesolve is called. A new matrix can be stored in an object
## if desired.
## makeCacheMatrix returns list of functions based on the initial input vector
## x. The list of functions that will be stored will (1) set a new matrix to
## invert (2) return the currently set matrix to be inverted (3) set the inverse
## once calculated (4) return the inverse of the set matrix
makeCacheMatrix <- function(x = matrix()) {
i <- NULL
set <- function(y) {
x <<- y
i <<- NULL
}
get <- function() x
setInverse <- function(inverse) i <<- inverse
getInverse <- function() i
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## cachesolve will take an input that will be an object created from
## makeCacheMatrix being called. If there is an inverse already calculated it
## will return that inverse, otherwise it will calculate the inverse of the
## currently set matrix.
cachesolve <- function(x, ...) {
i <- x$getInverse()
if(!is.null(i)) {
message("getting cached data")
return(i)
}
data <- x$get()
m <- solve(data, ...)
x$setInverse(i)
m
}