forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
68 lines (53 loc) · 2.03 KB
/
cachematrix.R
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
## Implementation of cached inverse matrix computation
## Usage example
## m <- matrix(runif(n=36), 6, 6) # create a random squared matrix
## mc <- makeCacheMatrix(m) # create the cached matrix
## cacheSolve(mc) # solve for inverse and save result to cached variable
## cacheSolve(mc) # second call with information message
## Creates a special "matrix" object that can cache its inverse.
## This special "matrix" is 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 matdix inverse
makeCacheMatrix <- function(x = matrix()) {
# initialize the inverse matrix with NULL
inverseMatrix <- NULL
# setter function; reset inverse matrix
set <- function(y) {
x <<- y
inverseMatrix <<- NULL
}
# getter function of matrix
get <- function() x
# setter inverse matrix
setinverse <- function(inverse) inverseMatrix <<- inverse
# getter inverse matrix
getinverse <- function() inverseMatrix
# list functions of makeCacheMatrix
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Computes the inverse of the special "matrix" returned by makeCacheMatrix.
## If the inverse has already been calculated (and the matrix has not changed),
## then the cachesolve should retrieve the inverse from the cache, otherwise the
## inverse is recalculated and cached.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
# get inverseMatrix from makeCacheMatrix
inverseMatrix <- x$getinverse()
# check if inverse matrix is already calculated and cached
if(!is.null(inverseMatrix)) {
message("getting cached data")
return(inverseMatrix)
}
# otherwise get matrix
data <- x$get()
# calculate inverse matrix with solve function
inverseMatrix <- solve(data, ...)
# set result into cached variable in makeCacheMatrix
x$setinverse(inverseMatrix)
# return inverseMatrix
inverseMatrix
}