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
36 lines (31 loc) · 992 Bytes
/
Copy pathcachematrix.R
File metadata and controls
36 lines (31 loc) · 992 Bytes
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
## The following functions create a cache for a matrix and for its inverse.
## They also allow for the calculation of the inverse and/or returning the
## pre-calculated inverse from cache
## Create a cache store for a matrix and its inverse
makeCacheMatrix <- function(x = matrix()) {
cachedinv <- NULL
set <- function(y) {
x <<- y
cachedinv <<- NULL
}
get <- function() x
setinverse <- function(mtx) cachedinv <<- mtx
getinverse <- function() cachedinv
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Calculate the inverse of the matrix if not already calculated.
## If calculated, return the cached result
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getmatrix()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data, ...)
x$setmatrix(inv)
inv
}