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

Skip to content

Commit ffee36e

Browse files
committed
Update cachematrix.R
Updates for Programming Assignment rdpeng#3
1 parent 7f657dd commit ffee36e

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

cachematrix.R

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,51 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## These functions work together to either solve for the inverse of a
2+
## matrix or to retrieve the inverse if it is already stored in memory.
33

4-
## Write a short comment describing this function
4+
## makeCacheMatrix creates a four step vector of functions for each scenario
5+
## that cacheSolve requires to store and retrieve the inverse from the cache.
56

67
makeCacheMatrix <- function(x = matrix()) {
7-
8+
m <- NULL
9+
10+
## Set Matrix in Cache to Null
11+
set <- function(y) {
12+
x <<- y
13+
m <<- NULL
14+
}
15+
16+
## Get Matrix
17+
get <- function() x
18+
19+
## Set Inverse in Cache
20+
setinverse <- function(inverse) m <<- inverse
21+
22+
## Get Inverse from Cache
23+
getinverse <- function() m
24+
25+
## Return list of functions
26+
list(set = set, get = get,
27+
setinverse = setinverse,
28+
getinverse = getinverse)
829
}
930

10-
11-
## Write a short comment describing this function
31+
## The cacheSolve function calculates the inverse of a given matrix or
32+
## retrieves the existing result from cache.
1233

1334
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
35+
## If m is not null return the cache data
36+
m <- x$getinverse()
37+
if(!is.null(m)) {
38+
message("getting cached data")
39+
return(m)
40+
}
41+
42+
## Otherwise calculate the matrix inverse
43+
data <- x$get()
44+
m <- solve(data, ...)
45+
46+
## And then store the results in the cache
47+
x$setinverse(m)
48+
49+
## Return the newly calculate inverse of the matrix
50+
m
1551
}

0 commit comments

Comments
 (0)