forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cachematrix.R
43 lines (37 loc) · 1.23 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
37
38
39
40
41
42
43
## Programming Assignment 2 -- Lexical Scoping.
##
## A pair of functions that cache the inverse of a matrix
## The function makeCacheMatrix create the matrix, cache its inverse
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
## set the matrix
set <- function(y) {
x <<- y
m <<- NULL
}
## get the matrix
get <- function() x
setmatrix <- function(solve) m <<- solve
getmatrix <- function() m
list(set = set, get = get,
setmatrix = setmatrix,
getmatrix = getmatrix)
}
## The function cacheSolve absorbs all arguments from makeCacheMatrix,
## check if the inverse has been calculated or not.
## This function assume that the matrix is always invertible.
## Calculate the inverse or get it from the cache if the matrix has not changed.
cacheSolve <- function(x = matrix(), ...) {
m <- x$getmatrix()
## if the inverse has been calculated, just return the matrix m
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
## get the inverse using the solve function
m <- solve(data, ...)
x$setmatrix(m)
m
## Return the matrix m that is the inverse of 'x'
}