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
36 lines (33 loc) · 1.16 KB
/
cachematrix.R
File metadata and controls
36 lines (33 loc) · 1.16 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
## This program will cache the inverse of a SQUARE matrix.
## Matrix inversion is usually very costly in computation.
## Thus it is benefitial to cache the inverse of a matrix and retrieve it when it is needed
## calculate it if it is new matrix.
## Nov. 23 2014
## makeCacheMatrix will create a special matrix with the given matrix
## It will return a list of 4 functions that are associated with a special matrix
makeCacheMatrix <- function(x = matrix()) {
v <- NULL
set <- function(y) {
x <<- y
v <<- NULL
}
get <- function() x
setinv <- function(inv) v <<- inv
getinv <- function() v
list(set = set, get = get, setinv = setinv, getinv = getinv)
}
## cacheSolve will return a matrix that is the inverse of 'x'
## It either get it from the cache if it exits
## or calculate it if it is new
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
v <- x$getinv()
if(!is.null(v)) {
message("getting cached data")
return(v)
}
data <- x$get()
v <- solve(data)
x$setinv(v)
v
}