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
52 lines (47 loc) · 1.15 KB
/
Copy pathcachematrix.R
File metadata and controls
52 lines (47 loc) · 1.15 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
## Functions to create the inverse of a matrix and cache the results
## makeCacheMatrix stores inverse of a matrix.
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
# setter for matrix
set <- function(y) {
x <<- y
m <<-NULL
}
# getter for matrix
get <- function() {
x
}
# set passed inverse to internal var
setsolve <- function(solve) {
m <<- solve
}
# return interal inverse var
getsolve <- function() {
m
}
# available functions
list(set = set, get = get,
setsolve = setsolve,
getsolve = getsolve)
}
## cacheSolve checks makeCacheMatrix for
## cached inverse value and returns it or generates
## new inverse and caches in makeCacheMatrix
cacheSolve <- function(x, ...) {
# get the cached value, if any
m <- x$getsolve()
# if there is a value, print and return
if(!is.null(m)) {
message("getting cached data")
return(m)
}
# If there was no cached version...
# get the data
data <- x$get()
# generate the solve
m <- solve(data, ...)
# and cache
x$setsolve(m)
# return m
m
}