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
54 lines (36 loc) · 1.3 KB
/
cachematrix.R
File metadata and controls
54 lines (36 loc) · 1.3 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
##Two functions: first one returns list of functions that
## invert matrix, gets the cache, sets the cache and gets all data
## the second function uses the first to solve the matrix and store
## the values
## Write a short comment describing this function
##functions to solve matrix, get cache, set cache and get data
makeCacheMatrix <- function(x = matrix()) {
#instantiate the matrix to store
cache <- NULL
#solve it
solveMatrix <- function(y) {
solve(y)
}
#get cache
getCache <- function() cache
#get data
getData <- function() x
#setCache
setCache <- function(y) {
cache <<- solveMatrix(y)
}
list (solveMatrix=solveMatrix, getCache=getCache, setCache=setCache, getData = getData)
}
## Write a short comment describing this function
## get cache; if not null, return it. if null, solve & store in cache
cacheSolve <- function(x, ...) {
cache <- x$getCache()
if(!is.null(cache)){
message("returning cache")
return(cache)
}
data <- x$getData()
solved <- x$solveMatrix(data)
x$setCache(data)
solved
}