Skip to content

Commit 4747535

Browse files
committed
updated cachematrix.R with the updated methods and functionality.
1 parent 22cb816 commit 4747535

File tree

1 file changed

+52
-33
lines changed

1 file changed

+52
-33
lines changed

cachematrix.R

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,59 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Ethan Bambock
2+
## R-Programming - Assignment 2
3+
## 2015/01/22
34

4-
## Write a short comment describing this function
5+
## This script provides a reuseable method for setting and getting a matrix and it's inverse values.
6+
## Additionally, the cached data can be retained using a global variable ("i") that doesn't have to
7+
## recalculated when the function getinverse() is called. This technique allows an application or process
8+
## to run more efficiently without having to process unnecessary work.
59

6-
makeCacheMatrix <- function(x = matrix()) {
10+
## This function is used to define a special 'matrix' object with a series of functions that can be
11+
## called to obtain or set a matrix value and cache it's inverse.
712

13+
makeCacheMatrix <- function(mx = matrix()) {
14+
15+
message("makecachematrix: initializing matrix object and functions")
16+
# initializing the inverse matrix object to NULL
17+
i <- NULL
18+
19+
# set() is used to store matrix passed as an argument or sets a default
20+
set <- function(y) {
21+
mx <<- y
22+
i <<- NULL
23+
}
24+
25+
# get() returns the value of the matrix that has been stored
26+
get <- function() mx
27+
28+
# setinverse() is used to store the inverse matrix 'globally' for the currently set matrix
29+
setinverse <- function(inverse) i <<- inverse
30+
31+
# getinverse() returns the inverse matrix that is currently stored 'globally'
32+
getinverse <- function() i
33+
34+
# last command evaluated and returns a list of the defined functions
35+
list(set = set, get = get, setinverse = setinverse, getinverse = getinverse)
836
}
937

1038

11-
## Write a short comment describing this function
39+
## This function is used to verify if a matrix inverse has been set and use the data stored. If
40+
## the inverse matrix returned from getmatrix() above is NULL, the data is obtained and the inverse
41+
## is calculated with the solve() function, stored, and returned.
1242

13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
15-
}
16-
makeVector <- function(x = numeric()) {
17-
m <- NULL
18-
set <- function(y) {
19-
x <<- y
20-
m <<- NULL
21-
}
22-
get <- function() x
23-
setmean <- function(mean) m <<- mean
24-
getmean <- function() m
25-
list(set = set, get = get,
26-
setmean = setmean,
27-
getmean = getmean)
28-
}
29-
30-
cachemean <- function(x, ...) {
31-
m <- x$getmean()
32-
if(!is.null(m)) {
33-
message("getting cached data")
34-
return(m)
35-
}
36-
data <- x$get()
37-
m <- mean(data, ...)
38-
x$setmean(m)
39-
m
40-
}
43+
cacheSolve <- function(mx, ...) {
44+
## Return a matrix that is the inverse of 'mx'
45+
i <- mx$getinverse()
46+
47+
# if the data returned is not null, return the value retrieved from getinverse()
48+
if(!is.null(i)) {
49+
message("cachesolve: getting cached inverse matrix")
50+
return(i)
51+
}
52+
53+
# otherwise cache the new data and return the value
54+
message("cachesolve: setting cached inverse matrix")
55+
data <- mx$get()
56+
i <- solve(data, ...)
57+
mx$setinverse(i)
58+
i
59+
}

0 commit comments

Comments
 (0)