|
183 | 183 | }
|
184 | 184 | cacheSolve(q)
|
185 | 185 | 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) |
0 commit comments