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
24 lines (24 loc) · 1.25 KB
/
Copy pathcachematrix.R
File metadata and controls
24 lines (24 loc) · 1.25 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
## the makeCacheMatrix function creates special matrix object that can cache its inverse
makeCacheMatrix <- function(x = matrix()) { ## sets x equal to an empty matrix
Inv <- NULL ## set inverse equal to NULL
set <- function(y) {
x <<- y ## set function assigns y argument equal to x
Inv <<- NULL ## reset of inverse to Null
}
get <- function() x ## return the matrix
setInv <- function(solve) Inv <<- solve ## setInv replaces the NULL value of Inv to the calculated inverse matrix
getInv <- function() Inv ## returns inverse matrix
list(set = set, get = get, setInv = setInv, getInv = getInv) ## creates list of the functions
}
## cacheSolve function returns the calculated inverse matrix, and if not already calculated, calculates the inverse of the new matrix
cacheSolve <- function(x, ...) {
Inv <- x$getInv() ## gets the most recent value for the inverse
if(!is.null(Inv)) { ## if the value of the inverse is not null, returns the already calculated inverse of matrix x
message("getting cached data")
return(Inv)
}
data <- x$get() ## if the value of the inverse is NULL, calculates the inverse of matrix x
Inv <- solve(data, ...)
x$setInv(Inv) ## sets inverse to newly calculated inverse of matrix x
Inv ## returns inverse matrix
}