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
47 lines (35 loc) · 1.45 KB
/
cachematrix.R
File metadata and controls
47 lines (35 loc) · 1.45 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
## The following functions allow for the creation of a matrix where the invers
## is cached after it has been calculated. This prevents the re-execution
## of a rather expensive operation.
## makeCacheMatrix creates a 'special' matrix object that stores the
## matrix as well as its inverce. Use this matrix with cacheSolve()
makeCacheMatrix <- function(m.orig = matrix()) {
m.result <- NULL #when created the result is not available
set <- function(m.set) {
m.orig <<- m.set #save the new matrix
m.result <<- NULL #clear the result
}
get <- function() m.orig #return the original
setresult <- function(result) m.result <<- result #save a result matrix
getresult <- function() m.result #return the result matrix
list(set = set, get = get,
setresult = setresult,
getresult = getresult)
}
## cacheSolve finds the inverse of the matrix created by makeCacheMatrix().
## The inverse of the matrix is only calculated once. On subsequent calls, the
## cached result is used.
cacheSolve <- function(x, ...) {
mcache <- NULL #Cached matrix is initially null
m <- x$getresult() #Get the cached matrix and check if it exists
if(!is.null(m)){
message("Using cached data")
return(m) #Use and return the cached matrix if found
}
# Cached result was not found... Calculate
data <- x$get() #Get the initial matrix
result <- solve(data, ...) #Solve it
x$setresult(result) #Cache it
result #Return it
}
#done