forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
44 lines (32 loc) · 919 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
37
38
39
40
41
42
43
44
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 of the matrix
setinverse <- function(inverse) inv <<- inverse
# get the value of the inverse of the matrix
getinverse <- function() inv
# return a list of the calculated values
list(set=set, get=get, setinverse=setinverse, getinverse=getinverse)
}
## Return a matrix that is the inverse of 'x'
cacheSolve <- function(x, ...) {
# check if the inverse is already computed
inv <- x$getinverse()
# return inverse if it's already been computed
if(!is.null(inv)) {
return(inv)
}
# compute the inverse of the matrix
data <- x$get()
inv <- solve(data)
# set the value in the cache
x$setinverse(inv)
# return the inverse
inv
}