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
49 lines (46 loc) · 1.48 KB
/
Copy pathcachematrix.R
File metadata and controls
49 lines (46 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
## Functions for caching the inverse of a matrix (using solve()), to avoid calculating it more than once
##
## Example usage:
## > m <- matrix(c(20, 0.5, 4, 9), nrow = 2, ncol = 2)
## > mycachematrix = makeCacheMatrix(m)
## > mycachematrix$get()
## [,1] [,2]
## [1,] 20.0 4
## [2,] 0.5 9
## > cacheSolve(mycachematrix)
## [,1] [,2]
## [1,] 0.050561798 -0.02247191
## [2,] -0.002808989 0.11235955
## > cacheSolve(mycachematrix)
## getting cached data
## [,1] [,2]
## [1,] 0.050561798 -0.02247191
## [2,] -0.002808989 0.11235955
## >
## Creates a special matrix, with functions to allow getting and setting its value and its inverse
makeCacheMatrix <- function(x = matrix()) {
storedInverse <- NULL
set <- function(newMatrix) {
x <<- newMatrix
storedInverse <<- NULL
}
get <- function() x
setInverse <- function(inverse) storedInverse <<- inverse
getInverse <- function() storedInverse
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## Gets the matrix inverse, either freshly calculated, or from cache if it has been calculated already
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
storedInverse <- x$getInverse()
if(!is.null(storedInverse)) {
message("getting cached data")
return(storedInverse)
}
data <- x$get()
newInverse <- solve(data, ...)
x$setInverse(newInverse)
newInverse
}