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
36 lines (31 loc) · 1.05 KB
/
Copy pathcachematrix.R
File metadata and controls
36 lines (31 loc) · 1.05 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
## Here, we are creating functions within functions to execute 4 primary tasks,
## namely,
## 1. Create a matrix/ Set the values of a matrix
## 2. Retrieve the matrix/ Get the values of the matrix
## 3. Create inverse of the matrix
## 4. Retrieve the value of inverse of the matrix/ Get the values of the inverse
## matrix
makeCacheMatrix <- function(x = matrix()) {
invers <- NULL
set <- function(y){
x <<- y
invers <<- NULL
}
get <- function() x
setInverse <- function(inverse) invers <<- inverse
getInverse <- function() invers
list(set=set, get=get, setInverse=setInverse, getInverse=getInverse)
}
## Here in 2nd function "invers" is the variable in which the inverse of the matrix is stored. So, we
## first check if "invers" is empty and hence accordingly print the output showing if ## the inverse matrix is cached.
cacheSolve <- function(x, ...) {
invers <- x$getInverse()
if(!is.null(invers)){
message("getting cached data")
return(invers)
}
outmat <- x$get()
invers <- solve(outmat,...)
x$setInverse(invers)
invers
}