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
33 lines (28 loc) · 1003 Bytes
/
cachematrix.R
File metadata and controls
33 lines (28 loc) · 1003 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
## The two functions below create a "smart" matrix, which cache its calculated
## inverse, rather than recompute it each time a user retrieves it
## The matrix factory. Creates the "smart" matrix, and allows editting of
## matrix content (which automatically deletes the inverse) and inverse and retrieval
## of both
makeCacheMatrix <- function(x = matrix()) {
i <- NULL
set <- function(y){
x <<- y
i <<- NULL
}
get <- function() x
setInverse <- function(inverse) i <<- inverse
getInverse <- function() i
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
}
## Returns the matrix inverse. Either from cache, or, if not cached,
## caculates it
cacheSolve <- function(x, ...) {
i <- x$getInverse()
if (!is.null(i)){
message("getting cached data")
return(i)
}
i <- solve(x$get(), ...)
x$setInverse(i)
i
}