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 (40 loc) · 1.63 KB
/
cachematrix.R
File metadata and controls
45 lines (40 loc) · 1.63 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 <- function(matrixInput = matrix()) {
##Initialize the matrix Inverse variable to NULL
## and then prepse set function to set the matrix input
matrixInverse <- NULL
set <- function(mInput) {
matrixInput <<- mInput
matrixInverse <<- NULL
}
##prepare get function to return the input matrix
get <- function() matrixInput
##set inverse to set the inverse matrix value in matrixInverse
## and getInerverse function will return the value
setInverse <- function(mInverse) matrixInverse <<- mInverse
getInverse <- function() matrixInverse
##finally make a list of all functions
list(set = set, get = get,setInverse = setInverse,getInverse = getInverse)
}
## Write a short comment describing this function
cacheSolve <- function(mtrxInput, ...) {
## Return a matrix that is the inverse of 'x'
## First check to seee if the inverse of the matrix is already computed
## So get the value from the getInverse function
matrixInverse <- mtrxInput$getInverse()
## Check if the returned value is NULL or not
## If not null retrieve the data from cached variable
if(!is.null(matrixInverse)) {
message("getting cached data")
return(matrixInverse)
}
## If the value of retrieved inverse is NULL, lets computer afresh now
## First get the matrix, and then solve for the inverse matrix
mtrx <- mtrxInput$get()
matrixInverse <- solve(mtrx)
## Finally the setInverse function of set the value
mtrxInput$setInverse(matrixInverse)
matrixInverse
}