forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
45 lines (38 loc) · 1.22 KB
/
cachematrix.R
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
## These functions will calculate the inverse of a matrix and
## store the result preventing the recalculation of a given matrix.
## makeCacheMatrix sets up a vector of functions which can
## store a matrix and store information about it in a retrieviable
## format.
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 checks the matrix made by makeCacheMatrix
## and either returns the inverse of 'x' if it has already
## been calculated or triggers the calculaton and returns
## the result.
cacheSolve <- function(x, ...) {
## Returns a matrix that is the inverse of 'x'
i <- x$getInverse()
## If the inverse of x has already been calculated
## return the result.
if(!is.null(i)) {
message("getting cached data")
return(i)
}
## If the inverse of x hasn't been calculated,
## caclulate it and store & return the result.
data <- x$get()
i <- solve(data, ...)
x$setInverse(i)
i
}