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
30 lines (26 loc) · 1.09 KB
/
Copy pathcachematrix.R
File metadata and controls
30 lines (26 loc) · 1.09 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
## It caches the inverse of a matrix. Whenever a inverse function is called, the## cache is first checked and then the value is retrieved based on it.
## Write a short comment describing this function
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL ## initialise inverse to NULL
set <- function(y){
x <<- y
inv <<- NULL ##setting inv to another environment
}
get <- function() x ##fetching value of matrix
setInverse <- function(solveMatrix) inv <<- solveMatrix ##sets inverse of matr##ix
getInverse <- function() inv ##fetch inverse of matrix
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
}
## Write a short comment describing this function
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getInverse() ##stores inverse in inv
if(!is.null(inv)){ ##checking if cache has some value or not
message("getting cached data")
return(inv)
}
data <- x$get() ##fetching matrix data
inv <- solve(data) ##calculating inverse
x$setInverse(inv) ##setting the inv variable with inverse
inv
}