Skip to content

Commit 75f0334

Browse files
committed
Update cachematrix.R
1 parent e4eed41 commit 75f0334

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

cachematrix.R

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
1+
## This function defines four fucntions to get and set the matrix and its inverse
2+
## This takes in the Matrix to be inverted as the input
73

4+
makeCacheMatrix <- function(x = matrix()) ## X is the input matrix
5+
{
6+
## Mt_Inv is the variable to hold out
7+
## Set to Null Initially
8+
Mt_Inv = NULL
9+
set = function(Mt_Set) #Function to reset the value of x - the input matrix
10+
{
11+
x <<- Mt_Set
12+
Mt_Inv <<- NULL
13+
}
14+
get = function() x #Function to get the value of x
15+
#Function to set the inverse value so its value can fetched without recalculating
16+
setInverse = function(solve) Mt_Inv <<- solve
17+
#Function to get inverse value if it is already set
18+
getInverse = function() Mt_Inv
19+
#Return a list of all above functions
20+
list(set = set, get = get, setInverse = setInverse,getInverse = getInverse)
821
}
922

1023

11-
## Write a short comment describing this function
1224

13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
25+
##Function to calculate inverse of the matrix inputted in the above function
26+
cacheSolve <- function(x, ...)
27+
{
28+
## Get the inverse matrix if it is already set
29+
Mt_Inv = x$getInverse()
30+
if (!is.null(Mt_Inv))
31+
{
32+
message("Cached Data")
33+
return (Mt_Inv)
34+
}
35+
## Caculate the inverse Matrix
36+
Mt_Inv = solve(x$get())
37+
## Set the inverse so that it can accessed without calculating the next time
38+
x$setInverse(Mt_Inv)
39+
return (Mt_Inv)
1540
}

0 commit comments

Comments
 (0)