Skip to content

Commit fd22a95

Browse files
committed
Assignment Complete
1 parent f14efe5 commit fd22a95

File tree

2 files changed

+78
-7
lines changed

2 files changed

+78
-7
lines changed

.Rhistory

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,71 @@ m
183183
}
184184
cacheSolve(q)
185185
cacheSolve(q)
186+
makeCacheMatrix <- function(x = matrix()) {
187+
m<-NULL
188+
set<-function(y){
189+
x<<-y
190+
m<<-NULL
191+
}
192+
get<-function() x
193+
setinverse<-function(inverse) m<<-inverse #Stores the inverse matrix in cache
194+
getinverse<-function() m #retrives the cached matrix
195+
list(set=set,get=get,setinverse=setinverse, getinverse=getinverse)
196+
}
197+
## This function checks to see if the inverse of the matrix is stored in cache. If the matrix
198+
## has already been calculated and stored, it will get it from the cache and display it. If
199+
## it has not been calculated, it will calculate the inverse, store it in memory, and display it.
200+
cacheSolve <- function(x) {
201+
## Return a matrix that is the inverse of 'x'
202+
m<-x$getinverse()
203+
if(!is.null(m)){
204+
message("Getting cached data")
205+
return(m)
206+
}
207+
data <-x$get()
208+
m<-solve(data) %*% #Creates the inverse matrix
209+
x$setinverse(m) #Stores the inverse matrix in cache
210+
m
211+
}
212+
c = rbind(c(1, -1/4), c(-1/4, 1))
213+
class(c)
214+
cacheSolve(c)
215+
makeCacheMatrix(c)
216+
cacheSolve(c)
217+
d<-makeCacheMatrix(c)
218+
cacheSolve(d)
219+
## This function will take a matrix as an argument and return the inverse of that matrix
220+
## The function will first check to see if the inverse matrix has already been cached
221+
## If the inverse already exists in the cache, the function will read and return it from the cache
222+
## If the inverse is not in the cache, the function will calculate the inverse and store it
223+
## in the cache.
224+
## This function creates a special matrix that will allow us to cache the inverse matrix and recall
225+
## it if the matrix inverse has already been calculated.
226+
makeCacheMatrix <- function(x = matrix()) {
227+
m<-NULL
228+
set<-function(y){
229+
x<<-y
230+
m<<-NULL
231+
}
232+
get<-function() x
233+
setinverse<-function(inverse) m<<-inverse #Stores the inverse matrix in cache
234+
getinverse<-function() m #retrives the cached matrix
235+
list(set=set,get=get,setinverse=setinverse, getinverse=getinverse)
236+
}
237+
## This function checks to see if the inverse of the matrix is stored in cache. If the matrix
238+
## has already been calculated and stored, it will get it from the cache and display it. If
239+
## it has not been calculated, it will calculate the inverse, store it in memory, and display it.
240+
cacheSolve <- function(x) {
241+
## Return a matrix that is the inverse of 'x'
242+
m<-x$getinverse()
243+
if(!is.null(m)){
244+
message("Getting cached data")
245+
return(m)
246+
}
247+
data <-x$get()
248+
m<-solve(data) %*%data #Creates the inverse matrix
249+
x$setinverse(m) #Stores the inverse matrix in cache
250+
m
251+
}
252+
cacheSolve(d)
253+
cacheSolve(d)

cachematrix.R

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,25 @@
44
## If the inverse is not in the cache, the function will calculate the inverse and store it
55
## in the cache.
66

7-
## Write a short comment describing this function
7+
## This function creates a special matrix that will allow us to cache the inverse matrix and recall
8+
## it if the matrix inverse has already been calculated.
89

910
makeCacheMatrix <- function(x = matrix()) {
10-
m<-NULL
11+
m<-NULL
1112
set<-function(y){
1213
x<<-y
1314
m<<-NULL
1415
}
1516
get<-function() x
16-
setinverse<-function(inverse) m<<-inverse
17-
getinverse<-function() m
17+
setinverse<-function(inverse) m<<-inverse #Stores the inverse matrix in cache
18+
getinverse<-function() m #retrives the cached matrix
1819
list(set=set,get=get,setinverse=setinverse, getinverse=getinverse)
1920
}
2021

2122

22-
## Write a short comment describing this function
23+
## This function checks to see if the inverse of the matrix is stored in cache. If the matrix
24+
## has already been calculated and stored, it will get it from the cache and display it. If
25+
## it has not been calculated, it will calculate the inverse, store it in memory, and display it.
2326

2427
cacheSolve <- function(x) {
2528
## Return a matrix that is the inverse of 'x'
@@ -29,7 +32,7 @@ cacheSolve <- function(x) {
2932
return(m)
3033
}
3134
data <-x$get()
32-
m<-solve(data)
33-
x$setinverse(m)
35+
m<-solve(data) %*%data #Creates the inverse matrix
36+
x$setinverse(m) #Stores the inverse matrix in cache
3437
m
3538
}

0 commit comments

Comments
 (0)