forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
39 lines (34 loc) · 1.17 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
## There are two functions defined below. makeCacheMatrix can be used
## to cache a matrix along with its inverse. cacheSolve returns the
## inverse of the matrix.
## makeCacheMatrix - a function that returns a holder object for a matrix
## and its inverse. The inverse is computed the first time and then cached
## for future reading. The object also returns functions to perform basic
## operations on the matrix, viz. get, set, getinv, setinv.
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
getinv <- function() inv
setinv <- function(z) inv <<- z
list( get = get, set = set, getinv = getinv, setinv = setinv )
}
## cacheSolve - this function returns the inverse of the matrix stored
## in the passed in object. It checks if the inverse has already been
## cached. If it has been cached, it returns the cached value,
## otherwise it computes the inverse and caches it before returning
## the value.
cacheSolve <- function(x) {
inv <- x$getinv()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data)
x$setinv(inv)
inv
}