|
1 | | -## Put comments here that give an overall description of what your |
2 | | -## functions do |
| 1 | +##Caching the Inverse of a Matrix |
3 | 2 |
|
4 | | -## Write a short comment describing this function |
| 3 | +######Example of using the function########## |
| 4 | +#x<-matrix(1:4,2,2) #Original Matrix creation |
| 5 | +#xin<-makeCacheMatrix(x) #Creates a special "Matrix" xin returns list of 4 functions |
| 6 | +#xin$get() #Return the original Matrix |
| 7 | +#cacheSolve(xin) #Compute and return the inverse of the original matrix |
| 8 | +#cacheSolve(xin) #Inverse already computed; show the message "getting cached data" and return the cached inverse matrix |
5 | 9 |
|
| 10 | +##In this example we use the <<- operator which assign a value to an object in an environment that is different from the current environment. |
| 11 | +##Below are two functions that are used to create a special object that stores a numeric Matrix and cache's its inverse |
6 | 12 |
|
7 | | -makeCacheMatrix <- function(x = matrix()) { |
| 13 | +##The first function, makeCacheMatrix creates a special "Matrix", which is really a list containing a function to |
| 14 | +##1) set the value of the matrix |
| 15 | +##2) get the value of the matrix |
| 16 | +##3) set the value of the inverse of the matrix |
| 17 | +##4) get the value of the inverse of the matrix |
8 | 18 |
|
| 19 | +makeCacheMatrix <- function(x = matrix()) { |
| 20 | + myinv <- NULL# myinv object will store the cached inverse of the matrix |
| 21 | + set <- function(y) { #Set the original Matrix |
| 22 | + x <<- y |
| 23 | + myinv <<- NULL |
| 24 | + } |
| 25 | + get <- function() x #Get the original Matrix |
| 26 | + setinvrse <- function(invrse) myinv <<- invrse #Set the inverse Matrix |
| 27 | + getinvrse <- function() myinv #Get the inverse Matrix |
| 28 | + #Return the list containing the functions |
| 29 | + list(set = set, get = get,setinvrse = setinvrse,getinvrse = getinvrse) |
9 | 30 | } |
10 | 31 |
|
11 | 32 |
|
12 | | -## Write a short comment describing this function |
| 33 | +##The following function, cacheSolve, calculates the inverse of the special "Matrix" created with the above function. |
| 34 | +##However, it first checks to see if the inverse has already been calculated. If so, it gets the inverse from the cache and skips the computation. |
| 35 | +##Otherwise, it calculates the inverse of the Matrix and sets the value of the inverse in the cache via the setinvrse function. |
13 | 36 |
|
14 | 37 | cacheSolve <- function(x, ...) { |
15 | | - ## Return a matrix that is the inverse of 'x' |
| 38 | + ## Return a matrix that is the inverse of 'x' |
| 39 | + myinv <- x$getinvrse() |
| 40 | + ##Checks to see if the inverse has already been calculated, if yes, return it |
| 41 | + if(!is.null(myinv)) { |
| 42 | + message("getting cached data") |
| 43 | + return(myinv) |
| 44 | + } |
| 45 | + ##It calculates the inverse of the Matrix |
| 46 | + data <- x$get() |
| 47 | + myinv <- solve(data, ...) |
| 48 | + ##Cache the inverse matrix |
| 49 | + x$setinvrse(myinv) |
| 50 | + ##Return the computed inverse matrix |
| 51 | + myinv |
16 | 52 | } |
0 commit comments