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
35 lines (26 loc) · 1.26 KB
/
cachematrix.R
File metadata and controls
35 lines (26 loc) · 1.26 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
## The first function creates a special matrix that stores its inverse
## The second function computes the inverse of the special "matrix" returned by makeCacheMatrix above.
## If the inverse has already been calculated the cached inverse is returned
## Returns a list containing four functions to get and set the data and the inverse
makeCacheMatrix <- function(x = matrix()) {
invertedMatrix <- NULL
get <- function() { x }
set <- function(newX) { x <<- newX ; invertedMatrix <<- NULL }
setInvertedMatrix <- function(newInvertedMatrix = matrix()) { invertedMatrix <<- newInvertedMatrix }
getInvertedMatrix <- function() { invertedMatrix }
list(get = get, set = set, getInvertedMatrix = getInvertedMatrix, setInvertedMatrix = setInvertedMatrix)
}
## Returns a matrix that is the inverse of 'x'
cacheSolve <- function(x, ...) {
matrixToBeInverted <- x$get()
cachedInvertedMatrix <- x$getInvertedMatrix()
## If the data are different the cachedInvertedMatrix would be NULL
## See set() function in the makeCacheMatrix() function
if (!is.null(cachedInvertedMatrix)) {
message("getting cached data")
return(cachedInvertedMatrix)
}
invertedMatrix <- solve(matrixToBeInverted)
x$setInvertedMatrix(invertedMatrix)
return(invertedMatrix)
}