forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
41 lines (39 loc) · 1.11 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
## Usage of the functions:
## > a <- makeCacheMatrix()
## > a$set(matrix(1:4,2,2))
## > cacheSolve(a)
## create a 'special' matrix object that can cache its inverse
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
## set the value of the matrix
set <- function(y) {
x <<- y
inv <<- NULL
}
## get the value of the matrix
get <- function() x
## set the value of the inverse
setinverse <- function(solve) inv <<- solve
## get the value of the inverse
getinverse <- function() inv
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Get the inverse of the 'special' matrix object created with 'makeCacheMatrix'
cacheSolve <- function(x = matrix(), ...) {
## check to see if the inverse has already been calculated
inv <- x$getinverse()
if(!is.null(inv)) {
message("getting inverse from cache")
}
else
{
## calculate the inverse
data <- x$get()
inv <- solve(data, ...)
## sets the value of the inverse in the cache
x$setinverse(inv)
}
inv
}