forked from gjmulder/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
44 lines (41 loc) · 903 Bytes
/
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
44
# A cacheable matrix mini-library
# Example usage:
#
# > m <- matrix(1:4, nrow=2, ncol=2)
# > cm <- makeCacheMatrix(m)
# > cacheSolve(cm)
# [,1] [,2]
# [1,] -2 1.5
# [2,] 1 -0.5
# > cacheSolve(cm)
# getting cached data
# [,1] [,2]
# [1,] -2 1.5
# [2,] 1 -0.5
# Create a cacheable matrix with associated functions to access it
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setinv <- function(inv) m <<- inv
getinv <- function() m
list(set = set, get = get,
setinv = setinv,
getinv = getinv)
}
# Return the inverse of the cacheable matrix
# If the inverse has been cached return the cached matrix
cacheSolve <- function(x) {
i <- x$getinv()
if(!is.null(i)) {
message("getting cached data")
return(i)
}
data <- x$get()
i <- solve(data)
x$setinv(i)
i
}