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
46 lines (36 loc) · 1.09 KB
/
cachematrix.R
File metadata and controls
46 lines (36 loc) · 1.09 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
## Put comments here that give an overall description of what your
## functions do
## Create the object that contains functions to set/get the matrix and the inverse
makeCacheMatrix <- function(x = matrix()) {
#Initialize the inverse
inverseMatrix <- NULL
# Set
set <- function(new){
x <<- new
inverseMatrix <<- NULL
}
# Get
get <- function() x;
setInverse <- function(inverse){
inverseMatrix <<- inverse
}
getInverse <- function() inverseMatrix
# Define the list that contains all functions defined earlier
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
}
## Calculate the inverse if it's not defined.
## If the inverse already exists, returns it
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
m <- x$getInverse()
# If the inverse is cached ...
if (!is.null(m)) {
message("getting cached inverse")
return(m)
}
# Calculate the inverse
data <- x$get();
inverseMatrix <- solve(data)
x$setInverse(inverseMatrix)
inverseMatrix
}