-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
26 lines (24 loc) · 1.06 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
#makeCacheMatrix function creates a special "matrix" object which enables us to cache the inverse of the matrix
makeCacheMatrix <- function(x = matrix()){ #defining the argument
inv<- NULL #assigning NULL to the inv variable which will hold the value of the inverse matrix
set<- function(y){ #setting the value of the matrix
x<<-y #assigning the value of matrix in parent environment
inv<<- NULL #inv reset to NULL in case of new matrix
}
get<- function() {x} #defining "get" function which returns the value of the matrix argument
setInverse<- function(inverse) {inv <<- inverse} #assigns value of inv in parent environment
getInverse<- function() {inv}
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
}
#cacheSolve function computes the inverse of the special "matrix" created by the makeCacheMatrix above
cacheSolve<- function(x, ...){
inv<- x$getInverse()
if(!is.null(inv)){
message("getting cached data")
return(inv)
}
mat<- x$get()
inv<- solve(mat, ...)
x$setInverse(inv)
inv
}