forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
51 lines (43 loc) · 1.93 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
## This file contains code that calculates and caches the inverse (xinverse) of a matrix (x) instead of computing it repeatedly
## functions included in this file are: makeCacheMatrix() and cacheSolve()
## The makeCacheMatrix() function creates a special "matrix" object that can cache its inverse.
makeCacheMatrix <- function(x = matrix()) {
# Assign xinverse with NULL
xinverse <- NULL
# set() makeCacheMatrix "x" value to be like "y" and set "xinverse" value to NULL
set <- function(y) {
x <<- y
xinverse <<- NULL
}
# get() will return "x" value when called
get <- function() x
# setInverse() will set the inverse value of the matrix provided by "solve" argument
setInverse <- function(solve) xinverse <<- solve
# getInverse() will return the inverse value stored in "xinverse"
getInverse <- function() xinverse
# List of functions in makeCacheMatrix() function
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## The cacheSolve() function computes the inverse of the special "matrix" returned by makeCacheMatrix above.
## If the inverse has already been calculated (and the matrix has not changed), then the cacheSolve should retrieve the inverse from the cache.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
# call getInverse Function and assign the value to "xinverse"
xinverse <- x$getInverse()
# condition checks if inverse is already set
if(!is.null(xinverse)) {
# if yes, write a message and return "xinverse"
message("getting cached data")
return(xinverse)
}
# get the value and assign it to data
data <- x$get()
# use solve to obtain the inverse of the matrix 'x'
xinverse <- solve(data, ...)
# call the function setInverse() to set the inverse value
x$setInverse(xinverse)
# return the inverse value
xinverse
}