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
65 lines (49 loc) · 1.51 KB
/
cachematrix.R
File metadata and controls
65 lines (49 loc) · 1.51 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
## Creates matrix cache and declares functions
##_________________________________________________________
makeCacheMatrix <- function(x = matrix())
{
## Create inverse and set to null for cache check
inverse <- NULL
## Sets the matrix "x" to argument "y", which then sets
## inverse NULL as it hasn't been calculated yet
set <- function(y)
{
x <<- y
inverse <<- NULL
}
## Gets 'special' matrix "x"
get <- function() x
## Sets the matrix "inverse" to the "arg" provided
setInverse <- function(arg)
{
inverse <<- arg
}
## Gets inverse
getInverse <- function() inverse
## Lists the data
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## Checks whether or not there is cached data:
## If yes: return cached data; Else calculate it
##_________________________________________________________
cacheSolve <- function(x, ...)
{
## Return a matrix that is the inverse of 'x'
inverse <- x$getInverse()
## Checks for null value, if not null then value is cached
if(!is.null(inverse))
{
message("Getting Cached Data")
return(inverse)
}
## Gets data from "x"
matrixToInverse <- x$get()
## Calculating inverse with solve function
inverse <- solve(matrixToInverse)
## Calling "X" function "setInverse" to store inverse
x$setInverse(inverse)
## Print/Return inverse
inverse
}