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
60 lines (36 loc) · 1.64 KB
/
Copy pathcachematrix.R
File metadata and controls
60 lines (36 loc) · 1.64 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
#The first function, makeMatrix creates a special "matrix",
#which is really a list containing a function to
#1.set the value of the matrix
#2.get the value of the matrix
#3.set the value of the mean
#4.get the value of the mean
makeCacheMatrix <- function(x = matrix()) {
mat <- NULL
set <- function(y) {
x <<- y
mat <<- NULL
}
get <- function() x
setInv <- function(Inve) mat <<- Inve
getInv <- function() mat
list(set = set, get = get,
setInv = setInv,
getInv = getInv)
}
## The following function calculates the mean of the special "matrix"
#created with the above function. However, it first checks to see if
#the mean has already been calculated. If so, it gets the mean from the cache and
#skips the computation. Otherwise, it
##calculates the mean of the data and sets the value of the mean in
#the cache via the setmean function.
cacheSolve <- function(x, ...) {
mat <- x$getInv()
if(!is.null(mat)) {
message("getting cached data")
return(mat)
}
data <- x$get()
mat <- solve(data)
x$setInv(mat)
mat
}