Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit af9dd65

Browse files
committed
Submitting solution for Programming Assignment 2.
1 parent 7f657dd commit af9dd65

File tree

4 files changed

+119
-9
lines changed

4 files changed

+119
-9
lines changed

cachematrix.R

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,68 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
1+
# The two functions here work together to allow cached values for the inversion
2+
# of a Matrix. The Matrix is created with makeCacheMatrix() which returns a List
3+
# of functions to use when manipulating the Matrix. The cacheSolve(matrix)
4+
# function will return the cached inversion or solve a new inversion if required.
55

6+
# The makeCacheMatrix() function creates a special matrix which is really a
7+
# list containing functions to:
8+
# * get the value of the matrix
9+
# * set the value of the matrix
10+
# * get the value of the solved matrix
11+
# * set the value of the solved matrix
12+
#
13+
# Returns:
14+
# A List of functions used to manipulate the matrix
15+
#
16+
# Sample Use:
17+
# x <- makeCacheMatrix()
18+
# x$set(matrix(c(1,2,11 ,12), ncol=2, nrow=2, byrow=TRUE)) sets the matrix.
19+
# x$get() returns the matrix data
20+
# x$getsolve() returns the inverted matrix
21+
# x$setsolve([inverted matrix]) sets, and caches, the inverted matrix.
622
makeCacheMatrix <- function(x = matrix()) {
7-
23+
# Intialize the solved matrix inversion variable
24+
s <- NULL
25+
# define the set() function to set the matrix value and re-set the inversion
26+
set <- function(y) {
27+
x <<- y
28+
s <<- NULL
29+
}
30+
# define the get() function to return the matrix
31+
get <- function() x
32+
# define the setsolve() function to set the solved matrix value
33+
setsolve <- function(slv) s <<- slv
34+
# define the getsolve() function to get the solved matrix value
35+
getsolve <- function() s
36+
# return a List of functions to use when manipulating the matrix
37+
list(set = set, get = get,
38+
setsolve = setsolve,
39+
getsolve = getsolve)
840
}
941

10-
11-
## Write a short comment describing this function
12-
42+
# cacheSolve() inverts the matrix created with makeCacheMatrix().
43+
# It will first check to see if the inverted matrix has already been
44+
# solved. If so, it returns the previously inverted matrix. Otherwise,
45+
# it solves the inversion and saves it.
46+
#
47+
# Returns:
48+
# An inverted matrix.
49+
#
50+
# Sample Use (x is created with the above makeCacheMatrix()):
51+
# inversion <- cacheSolve(x) will return the cached inversion or solve new.
1352
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
53+
# Get the previously solved value
54+
s <- x$getsolve()
55+
# return the solved value if we have one. Give a message too.
56+
if(!is.null(s)) {
57+
message("getting cached data")
58+
return(s)
59+
}
60+
# We need to solve the matrix inversion. So get the data..
61+
data <- x$get()
62+
# Solve it..
63+
s <- solve(data, ...)
64+
# and set the value
65+
x$setsolve(s)
66+
# Return a matrix that is the inverse of 'x'
67+
s
1568
}

cachematrix_test.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
source('cachematrix.R')
2+
3+
x <- makeCacheMatrix()
4+
x$set(matrix(c(1,2,11,12), ncol=2, nrow=2, byrow=TRUE))
5+
message("Matrix:")
6+
print(x$get())
7+
message("Solving...")
8+
print(cacheSolve(x))
9+
message("and again...")
10+
print(cacheSolve(x))

cachemean.R

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# The first function, makeVector creates a special "vector", which is really a list containing a function to
2+
#
3+
# set the value of the vector
4+
# get the value of the vector
5+
# set the value of the mean
6+
# get the value of the mean
7+
8+
makeVector <- function(x = numeric()) {
9+
m <- NULL
10+
set <- function(y) {
11+
x <<- y
12+
m <<- NULL
13+
}
14+
get <- function() x
15+
setmean <- function(mean) m <<- mean
16+
getmean <- function() m
17+
list(set = set, get = get,
18+
setmean = setmean,
19+
getmean = getmean)
20+
}
21+
22+
# The following function calculates the mean of the special "vector" created with the above function. However, it first checks to see if the mean has already been calculated. If so, it gets the mean from the cache and skips the computation. Otherwise, it calculates the mean of the data and sets the value of the mean in the cache via the setmean function.
23+
24+
cachemean <- function(x, ...) {
25+
m <- x$getmean()
26+
if(!is.null(m)) {
27+
message("getting cached data")
28+
return(m)
29+
}
30+
data <- x$get()
31+
m <- mean(data, ...)
32+
x$setmean(m)
33+
m
34+
}

cachemean_test.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
source('cachemean.R')
2+
3+
x <- makeVector()
4+
print("Vector Empty")
5+
print(cachemean(x))
6+
print("and again...")
7+
print(cachemean(x))
8+
print('')
9+
print("Vector 1:50")
10+
x$set(1:50)
11+
print(cachemean(x))
12+
print("and again...")
13+
print(cachemean(x))

0 commit comments

Comments
 (0)