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
38 lines (32 loc) · 1.08 KB
/
Copy pathcachematrix.R
File metadata and controls
38 lines (32 loc) · 1.08 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
# In this R script, there are two functions: makeCaheMatrix and cacheSolve.
# The aim is to calculate the inversion of a matrix.
# To ensure the code stays efficient in the case of large more complex matricies,
# the code can cache the value of the matrix inversion, so that when needed again,
# it can be looked up in the cache rather than recomputed.
## Return a list(of functions) that can: get / set the matrix, or get / set the inverse of the matrix
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setMatrix <- function(solve) m <<- solve
getMatrix <- function() m
list(set = set, get = get,
setMatrix = setMatrix,
getMarix = getMatrix)
}
## Compute the inverse of a matrix, but first check if its inverse is stored in cache.
cacheSolve <- function(x = matrix(), ...) {
## Return a matrix that is the inverse of 'x'
m <- x$getMatrix()
if(!is.null(m)) {
message("Getting the cached data")
return(m)
}
matrix <- x$get()
m <- solve(matrix, ...)
x$setMatrix(m)
m
}