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
37 lines (32 loc) · 1.29 KB
/
Copy pathcachematrix.R
File metadata and controls
37 lines (32 loc) · 1.29 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
## The first functions takes a matrix as and input and creates a cache that can be written, along
## with methods(functions) that can be applied to the special object. The second function takes the
## output of the first function and check if a cache exists. If not, it writes a new one with the
## inverse matrix.
## makeCacheMatrix creates a special "matrix" list: a matrix with a cache and a list of functions
## in order to set and get either the matrix or its cache
makeCacheMatrix <- function(x = matrix()) {
cacheMatrix <- NULL
set <- function(y) {
x <<- y
cacheMatrix <<- NULL
}
get <- function() x
setInverse <- function(solve) cacheMatrix <<- solve
getInverse <- function() cacheMatrix
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## cacheSolve checks the special "matrix" object for a cached inverse, and if it exists, it gets it.
## Otherwise it solves for the inverse matrix
cacheSolve <- function(x, ...) {
cacheMatrix <- x$getInverse()
if(!is.null(cacheMatrix)) {
message("Getting cached matrix data")
return(cacheMatrix)
}
data <- x$get()
cacheMatrix <- solve(data, ...)
x$setInverse(cacheMatrix)
cacheMatrix
}