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
67 lines (53 loc) · 1.66 KB
/
cachematrix.R
File metadata and controls
67 lines (53 loc) · 1.66 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
# makeCacheMatrix returns a list of functions
# setMatrix
# getMatrix
# cacheInverse
# getInverse
# Its puspose is to store a matrix and a cached value of the inverse of the
# matrix.
makeCacheMatrix <- function(x = matrix()) {
# holds the cached value or NULL if nothing is cached
# initially nothing is cached so set it to NULL
cache <- NULL
# store a matrix
setMatrix <- function(newValue) {
x <<- newValue
# since the matrix is assigned a new value, flush the cache
cache <<- NULL
}
# returns the stored matrix
getMatrix <- function() {
x
}
# cache the given argument
cacheInverse <- function(solve) {
cache <<- solve
}
# get the cached value
getInverse <- function() {
cache
}
# return a list. Each named element of the list is a function
list(setMatrix = setMatrix, getMatrix = getMatrix, cacheInverse = cacheInverse, getInverse = getInverse)
}
## Write a short comment describing this function
## Return a matrix that is the inverse of 'x'
#note this function needs to be passed a data frame matrix
cacheSolve <- function(x, ...) {
inv <- x$getInverse()
#returns a cached value if it exists
if(!is.null(inv)){
message("getting cached data")
return(inv)
}
#otherwise we get the matrix, calculate its inverse
#and store it in the cache
data<-x$getMatrix()
inv <-solve(data)
y$cacheInverse(inv)
#return the inverse
inv
}