Skip to content

Commit fc84e7b

Browse files
committed
Update cachematrix.R
1 parent c11a15b commit fc84e7b

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

cachematrix.R

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,52 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
##Caching the Inverse of a Matrix
32

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
59

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
612

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
818

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)
930
}
1031

1132

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.
1336

1437
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
1652
}

0 commit comments

Comments
 (0)