forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
36 lines (33 loc) · 1.06 KB
/
cachematrix.R
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
## makeCacheMatrix stores matrix and its inverse calculated by cacheSolve
## cacheSolve returns inverse of matrix trying to get cached value first
## makeCacheMatrix returns a list with 4 functions
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setsolve <- function(solved) m <<- solved
getsolve <- function() m
list(set = set, get = get,
setsolve = setsolve,
getsolve = getsolve)
}
## cacheSolve returns inverse of matrix with cached result
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
## trying to get stored result
m <- x$getsolve()
## if m exists, no calculation
if (!is.null(m)) {
message("getting cached data")
return(m)
}
## here, inverse is not calculated
data <- x$get()
m <- solve(data, ...)
## stores result so it isn't calculated each time cacheSolve is called
x$setsolve(m)
m
}