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
65 lines (58 loc) · 1.44 KB
/
cachematrix.R
File metadata and controls
65 lines (58 loc) · 1.44 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
## The following pair of functions makes it possible to cache the
## value of the time-consuming operation of matrix inversion and
## can be used as follows:
##
## > A = matrix(
## + c(1, 2, 3, 4),
## + nrow=2,
## + ncol=2)
##
## > cA = makeCacheMatrix(A)
## > cA$get()
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
##
## > IA = cacheSolve(cA)
##
## > IA
## [,1] [,2]
## [1,] -2 1.5
## [2,] 1 -0.5
##
## To see the effect of caching, create a large sample matrix:
## > B = matrix(sample(1000,1000000,T),1000)
## > cB = makeCacheMatrix(B)
## > IB = cacheSolve(cB)
## (can take some time to compute depending on your hardware ...)
## > IB = cacheSolve(cB)
## getting cached data
## (the seconbd call returns the cached inverted matrix at once)
##
## Creates a cached matrix representation whereby the inverese
## of the matrix can be cached for fast retrieval
makeCacheMatrix <- function(x = matrix()) {
i <- NULL
set <- function(y) {
x <<- y
i <<- NULL
}
get <- function() x
setinv <- function(inverse) i <<- inverse
getinv <- function() i
list(set = set, get = get, setinv = setinv, getinv = getinv)
}
## Returns inverse of the matrix
## supplied as cached matrix representation
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
i <- x$getinv()
if(!is.null(i)) {
message("getting cached data")
return(i)
}
data <- x$get()
i <- solve(data, ...)
x$setinv(i)
i
}