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
50 lines (44 loc) · 1.6 KB
/
Copy pathcachematrix.R
File metadata and controls
50 lines (44 loc) · 1.6 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
38
39
40
41
42
43
44
45
46
47
48
49
50
## There are two functions in this file: makeCacheMatrix, cacheSolve
## makeCacheMatrix prepares a regular matrix to be used by cacheSolve.
## cacheSolve check the input and see if cached solution exist. If so, the
## cached solution is used, otherwise, it solves the matrix and store the
## solution in the cache.
## Look for a script called test.R in the repo, if you want to verify the
## script
## Return a list based upon the input matrix x to be used by cacheSolve()
makeCacheMatrix <- function(x = matrix()) {
## initialize the internal variable inv which holds the inverse
inv <- NULL
## set the value of the matrix and initialize the inverse
set <- function(y){
x <<- y
inv <<- NULL
}
## get the value of the matrix
get <- function() x
## set the value of the inverse
setinverse <- function(inverse) inv <<- inverse
## get the value of the inverse
getinverse <- function() inv
## return a list with the functions.
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Solve a matrix in a list prepared by makeCacheMatrix
cacheSolve <- function(x, ...) {
## get the inverse of the matrix in list x
inv <- x$getinverse()
## if the cached inverse exists, use it.
if(!is.null(inv)) {
message("getting cached data")
## return the cached copy of the inverse
return(inv)
}
## cached copy does not exist, calculate the inverse and store it.
data <- x$get()
inv <- solve(data, ...)
x$setinverse(inv)
## return the calculated inverse
inv
}