forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
36 lines (33 loc) · 908 Bytes
/
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
## Creates a special "matrix" object that can cache its inverse.
makeCacheMatrix <- function(x = matrix()) {
inverse <- NULL
## modify the reference of matrix
set <- function(newReference){
x <<- newReference
inverse <<- NULL
}
## get the matrix
get <- function() x
## cache the inverse
setinverse <- function(inverse) inverse <<- inverse
## get the cached inverse
getinverse <- function() inverse
list(set= set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Computes the inverse. If the inverse has been cached returns the cached value
cacheSolve<- function(x, ...) {
i <- x$getinverse()
## If matrix is cached already return cached data
if (!is.null(i)){
message("getting cached data")
return(i)
}
## calculate the inverse
data <- x$get()
i <- solve(data, ...)
## cache inverse
x$setinverse(i)
i
}