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
69 lines (47 loc) · 1.48 KB
/
cachematrix.R
File metadata and controls
69 lines (47 loc) · 1.48 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
## These two functions are used to create a special object
## that stores a matrix and cache it's inverse
## This function creates a special "matrix" which is a list
## of 4 functions that are used to
## - get / set the matrix
## - get / set the inverse of the matrix
makeCacheMatrix <- function(x = matrix()) {
# Used to store the inverse
m <- NULL
# Setter for the matrix
set <- function(y) {
#Store the matrix in a different environnement
x <<- y
# When new data is set the inverse must be reinitialized
m <<- NULL
}
# Getter for the matrix
get <- function() x
# Setter for the inverse
setinverse <- function(inverse) m <<- inverse
# Getter for the inverse
getinverse <- function() m
# Return all 4 functions in a list
list(set = set,
get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Return a matrix that is the inverse of 'x'
cacheSolve <- function(x, ...) {
# Get the cached inverse
m <- x$getinverse()
# If the inverse is cached
if(!is.null(m)) {
message("getting cached data")
# Return the cached value of the matrix and exit the function
return(m)
}
# Get the original matrix
data <- x$get()
# Solve its inverse
m <- solve(data, ...)
# Cache the inverse
x$setinverse(m)
# Return the inverse
m
}