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
43 lines (38 loc) · 1.17 KB
/
Copy pathcachematrix.R
File metadata and controls
43 lines (38 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
## With these functions you can make matrices with cached
## invert. So you can solve a matrix only once, and when you next
## ask for the invert, it will not compute it, rather it will get
## it from the cache.
## The function with which you can make a Matrix object
## what can cache its own invert.
makeCacheMatrix <- function(myMatrix = matrix()) {
cachedVal <- NULL
set <- function(newMatrix=matrix()) {
myMatrix <<- newMatrix
cachedVal <<- NULL
}
get <- function() {
myMatrix
}
setInvert <- function(invert) {
cachedVal <<- invert
}
getInvert <- function() {
cachedVal
}
list(set = set, get = get,
setInvert = setInvert,
getInvert = getInvert)
}
## Generates the invert of the matrix, and store it in the cache,
## or it gives back the value from cache if it's already generated.
cacheSolve <- function(myMatrix, ...) {
cachedVal <- myMatrix$getInvert()
if(!is.null(cachedVal)) {
message("getting cached data")
return(cachedVal)
}
data <- myMatrix$get()
cachedVal <- solve(data, ...)
myMatrix$setInvert(cachedVal)
cachedVal
}