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
49 lines (39 loc) · 899 Bytes
/
cachematrix.R
File metadata and controls
49 lines (39 loc) · 899 Bytes
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
## Matrix object and cached version for inverse function
##
## Example usage:
## x <- makeCacheMatrix()
## m <- matrix(c(2, 4, 3, 1), nrow=2, ncol=2)
## x$set(m)
## x$get()
## cacheSolve(x) # first run can be slow
## Constructor for the matrix with cached inverse
makeCacheMatrix <- function(x = matrix()) {
ci <- NULL
set <- function(y) {
x <<- y
ci <<- NULL
}
get <- function() {
x
}
setinverse <- function(inverse) {
ci <<- inverse
}
getinverse <<- function() {
ci
}
list(set = set, get = get,
setinverse = setinverse, getinverse = getinverse)
}
## returns the inverse of the matrix, result is cached after the first run
cacheSolve <- function(x, ...) {
ci <- x$getinverse()
if (!is.null(ci)) {
message("returning inverse matrix from cache")
return(ci)
}
m <- x$get()
ci <- solve(m, ...)
x$setinverse(ci)
ci
}