forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
49 lines (38 loc) · 1.17 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
46
47
48
49
## Two functions to cache the inverting of matrices. One to create
## 'objects' that have cacheable inverses and one to solve them
## using the cached result where available
## Creates special cacheable matrices from regular ones
makeCacheMatrix <- function(x = matrix()) {
# i will hold the inverse
i <- NULL
# local function to set the matrix and reset the inverse
set <- function(y) {
x <<- y
i <<- NULL
}
# getter for the actual matrix
get <- function() x
# setter and getter for the matrix inverse
setinverse <- function(inverse) i <<- inverse
getinverse <- function() i
# return value is a list of the local functions
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
# Caches the inverse of a 'cachematrix' as created
# by the function above
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
i <- x$getinverse()
# If the passed matrix already has return it
if(!is.null(i)) {
message("getting cached data")
return(i)
}
# Get the underlying matrix, solve it and set it for re-use later
data <- x$get()
m <- solve(data)
x$setinverse(m)
m
}