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
38 lines (32 loc) · 1.05 KB
/
cachematrix.R
File metadata and controls
38 lines (32 loc) · 1.05 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
## Two functions that together invert a matrix once and cache it so that it
## doesn't need computing again
#Call function below first - set its value to an object. Only need to call this
# function once for a given matrix
makeCacheMatrix <- function(x = matrix()) {
inv.x <- NULL
set <- function(y) {
x <<- y
inv.x <<- NULL
}
get <- function() x
set.inverse <- function(inverse) inv.x <<- inverse
get.inverse <- function() inv.x
list(set=set,get = get,
set.inverse = set.inverse,
get.inverse = get.inverse)
}
## Call this function using as argument the object assigned from
## function above. Keep calling this function everytime you want to invert
## the same matrix. Second time it just reads in the cache.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv.x <- x$get.inverse()
if(!is.null(inv.x)) {
message("getting cached data")
return(inv.x)
}
mat <- x$get()
inv.x <- solve(mat, ...)
x$set.inverse(inv.x)
inv.x
}