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
47 lines (40 loc) · 1.71 KB
/
cachematrix.R
File metadata and controls
47 lines (40 loc) · 1.71 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
## Function "makeCacheMatrix" creates a list containing functions to:
## 1. set the values of the matrix
## 2. get the values of the matrix
## 3. set the values of the inverse matrix
## 4. get the values of the inverse matrix
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y){ ## set function sets values of the matrix
x <<- y
inv <<- NULL
}
get <- function() { ## get values of matrix x
x
}
setinv <- function(inverse){ ## set inverse matrix
inv <<- inverse
}
getinv <- function(){ ## get inverse matrix
inv
}
list(set = set, get = get, setinv = setinv, getinv = getinv)
}
## The following function "cacheSolve" returns the inverse of a matrix 'x' of
##the matrix in the "makeCacheMatrix" function. It first checks to see if the
## inverse has been calculated, and if so, returns the inverse matrix stored in
## the cache. If the inverse matrix has not been stored in the cache, it uses
## the Solve function to calculate the inverse, and will set the inverse in the
## cache via the 'setinverse' function.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getinv()
if(!is.null(inv)) { ## check if inverse is cached
message("getting cached data")
return(inv) ## if inverse is cached, return inverse and
} ## exit program
data <- x$get()
inv <- solve(data, ...) ## if inverse is not in cache, get matrix
x$setinv(inv) ## data, compute inverse of matrix, and call
inv ## set function to store inverse in cache
}