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
44 lines (39 loc) · 1.17 KB
/
cachematrix.R
File metadata and controls
44 lines (39 loc) · 1.17 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
## This function takes a square matrix as its argument and creates a special matrix
## object (a list of 4 functions) storing this matrix.
##
## Examples:
## The following creates a 3 by 3 matrix containing random values:
## matrix.object <- makeCacheMatrix(matrix(nrow = 3, ncol = 3, rnorm(9)))
##
## This prints the matrix:
## matrix.object$get()
makeCacheMatrix <- function(x = matrix()) {
i <- NULL
set <- function(y) {
x <<- y
i <<- NULL
list(set = set, get = get)
}
get <- function() x
setinv <- function(inverse) i <<- inverse
getinv <- function() i
list(set = set, get = get,
setinv = setinv,
getinv = getinv)
}
## This function calculates and stores the cached inverse matrix of a special matrix
## object, if it has not been stored yet, and the returns it.
## If there already exists a cached value, returns it without calculating it again.
##
## Use: cacheSolve(matrix.object)
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
}