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
48 lines (38 loc) · 1.2 KB
/
cachematrix.R
File metadata and controls
48 lines (38 loc) · 1.2 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
41
42
43
44
45
46
47
48
## Functions to create a matrix object and cache the inverse of the matrix
## to reduce compute time in case the inverse matrix needs to be calculated often
## This is the function that takes a matrix and creates an R object
## that includes the inverse of the matrix and functions to get and set
## the matrix and also get and set the cached inverse matrix
makeCacheMatrix <- function(x = matrix())
{
inv <<- NULL
set <- function(y)
{
x <<- y
m <<- NULL
}
get <- function(){ x }
setInverse <- function(inverse) { inv <<- inverse }
getInverse <- function() { inv }
list(set=set, get=get,
setInverse=setInverse,
getInverse=getInverse)
}
## This function does the actual invertion of the matrix. It checks to see if the
## inverse matrix has already been cached. If it has it returns the cached inverse
## matrix otherwise it creates the inverse matrix and then calls
## the setInverse function of the makeCacheMatrix object to set the cached inverse
## matrix
cacheSolve <- function (x, ...)
{
inv <- x$getInverse()
if(!is.null(inv))
{
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data)
x$setInverse(inv)
inv
}