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
41 lines (39 loc) · 1.41 KB
/
cachematrix.R
File metadata and controls
41 lines (39 loc) · 1.41 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
## 'makeCacheMatrix' creates a special "vector", which is really a list containing functions to
## set the value of the matrix
## get the value of the matrix
## set the value of the matrix inverse
## get the value of the matrix inverse
makeCacheMatrix <- function(x = matrix()) {
inverse <- NULL
# set the matrix value
set <- function(matrixToStore) {
x <<- matrixToStore
inverse <<- NULL
}
# get the matrix value
get <- function() x
# set the calculated matrix inverse
setinverse <- function(inv) inverse <<- inv
# get calculated matrix inverse
getinverse <- function() inverse
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## 'cacheSolve' function returns a matrix that is the inverse of 'x'
## if the matrix inverse is not found (first call) it calculates the inverse
## for the given matrix 'x', stores the result in cache and returns it.
## Every subsequent call will return already calculated inverse stored in cache
cacheSolve <- function(x = matrix(), ...) {
## check if the inverse is available and return it if it already exists
matrixInverse <- x$getinverse()
if(!is.null(matrixInverse)) {
message("getting cached data")
return(matrixInverse)
}
## if the inverse was not found in cache calculate it, store for later calls and return
data <- x$get()
matrixInverse <- solve(data, ...)
x$setinverse(matrixInverse)
matrixInverse
}