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
45 lines (36 loc) · 1.47 KB
/
cachematrix.R
File metadata and controls
45 lines (36 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
45
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
## makeCacheMatrix will create the special R vector which will contain
## the functions to get, set matrix as well as inverse of matrix
## also it will contain two objects x which is original matrix as well as
## inv which is inverese of matrix
makeCacheMatrix <- function(x = matrix()) {
inv<- NULL
set <- function(y){
x <<- y ## set the value of x in parent environment
inv <<- NULL ## set the value of inv in parent environment
}
get <- function() x ## returns value of x
setInv <- function(inverse){
inv <<- inverse ## set the value of inv in parent environment
}
getInv <- function() inv ## returns value of inv
list( set = set, get = get,
setInv = setInv, getInv = getInv) ## Create the special vector
}
## Write a short comment describing this function
##CacheSolve will check if x contains a matrix inverse cached or not
##if not then it will calculate the inverse and save it in the x
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
Inv <- x$getInv()
if (!is.null(Inv)){ ## if inverse is present or not
message("Getting Cached Data")
return(Inv) ## returns the cached inverse matrix
}
mat <- x$get()
Inv <- solve(mat, ...)
x$setInv(Inv)
Inv ## returns the freshly calculated inverse matrix
}