|
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 |
7 | 3 |
|
| 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) |
8 | 21 | }
|
9 | 22 |
|
10 | 23 |
|
11 |
| -## Write a short comment describing this function |
12 | 24 |
|
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) |
15 | 40 | }
|
0 commit comments