Skip to content

Commit fab0bc9

Browse files
authored
Committed changes for peer review of the week 3 assignment
1 parent 7f657dd commit fab0bc9

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

cachematrix.R

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Assignment for Week 3. Looking at Lexical Scoping
2+
## Objective is to write 2 functions that create a matrix and store it, then inverse
3+
## the matrix, but checking for its precense of a cached matrix first, before creating one.
34

4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
5+
## The first function will create an invertible matrix, get the inverse and store the inverse for later use.
76

7+
makeCacheMatrix <- function(x = matrix()) { ## setting the argument with mode of matrix
8+
inv <- NULL ## establishing variable to hold the inverse of the matrix
9+
set <- function(y){ ## giving the matrix in the parent environment a value
10+
x <<- y
11+
inv <<- NULL
12+
}
13+
get <- function() x ## returning the matrix value
14+
setInverse <- function(solveMatrix) inv <<- solveMatrix ## establishing the inverse value
15+
getInverse <- function() inv ## calls or gets the inverse value
16+
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse) ## making the functions available to be called later
817
}
918

1019

11-
## Write a short comment describing this function
20+
## determining the inverse of the matrix created in the MakeCacheMatrix function.
21+
## If the inverse is in the cache, then use that value, otherwise get the inverse.
1222

1323
cacheSolve <- function(x, ...) {
1424
## Return a matrix that is the inverse of 'x'
25+
inv <- x$getInverse() ## populate the inv variable with the value from the previous function,.
26+
if(!is.null(inv)){ ## if the variable is not null then retrieve the cache value.
27+
message("getting cached data")
28+
return(inv)
29+
}
30+
data <- x$get() ## If the inv variable is null, generate the inverse and set to inv variable.
31+
inv <- solve(data)
32+
x$setInverse(inv)
33+
inv
1534
}

0 commit comments

Comments
 (0)