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
44 lines (39 loc) · 1.47 KB
/
cachematrix.R
File metadata and controls
44 lines (39 loc) · 1.47 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
## A pair of functions are provided to calculate the inverse of a
## square matrix, and store this result. The stored result will be recalled
## if an inverse of an identical matrix is needed. Use makeCacheMatrix first.
# makeCacheMatrix
# this function creates a special list object to contain references to functions
# that provide the matrix to be inverted (get, called as x$get), the inversion
# (inv,called as x$getinv), and a function to set the inversion (setinv).
makeCacheMatrix <- function(x = matrix()) {
#makes sure we have an empty variable
i <- NULL
## Create a list to store values
set <- function(y) {
x <<- y #Link to object
i <<- Null #Holder for the inverse
}
get <- function() x
setinvers <- function(solve) i <<- solve
getinvers <- function() i
list(set = set, get = get,
setinvers = setinvers,
getinvers = getinvers)
}
## this function checks to see if the inverse of square matrix x has been calculated,
## and if not, returns the inverse through the solve function. If the inverse
## has been calculated previously, it returned the cached value (inv).
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
## Attempts to get the casched value if it exits return it
i <- x$getinvers()
if(!is.null(i)) {
message("getting cached data")
return(i)
}
## If it does not exits set calculate the value and save to I and return
data <- x$get()
i <- solve(data )
x$setinvers(i)
i
}