forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
43 lines (32 loc) · 1.18 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
## Put comments here that give an overall description of what your
## functions do
## creates a matrix object that caches its inverse
makeCacheMatrix <- function(x = matrix()) {
inverse <- NULL
set <- function(y){
x <<- y
inverse <<- NULL
}
get <- function() x
setinverse <- function(inverse) inverse <<-inverse
getinverse <- function () inverse
list(set=set, get=get, setinverse=setinverse, getinverse=getinverse)
}
## computes the inverse of the matrix from above. If the inverse has already,
## been calculated, it uses the value in the cache
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inverse <- x$getinverse()
#if inverse was already calculated
if(!is.null(inverse)){
#skip computation
message("...getting cached data")
return(inverse)
}
#else calculate the inverse
matrix.data <- x$get()
inverse = solve(matrix.data, ...)
#cache the inverse value
x$setinverse(inverse)
return(inverse)
}