Skip to content

Commit 0852332

Browse files
cached matrix written
1 parent 7f657dd commit 0852332

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

cachematrix.R

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,44 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
1+
## This collection of functions gives us the ability to make more
2+
## efficient calls to retrieve an inverted matrix by caching the inverse
73

4+
## creates a special matrix that caches its inverse
5+
makeCacheMatrix <- function(m = matrix()) {
6+
cachedInverse <- NULL
7+
8+
set <- function(newmatrix)
9+
{
10+
m <<- newmatrix
11+
cachedInverse <<- NULL
12+
}
13+
setinverse <- function(mi)
14+
{
15+
cachedInverse <<- mi
16+
}
17+
getinverse <- function()
18+
{
19+
cachedInverse
20+
}
21+
22+
list(set = set, get=get, setinverse = setinverse, getinverse = getinverse)
823
}
924

1025

11-
## Write a short comment describing this function
26+
## Invert the matrix m, where m is a special matrix created by
27+
## the above makeCacheMatrix. The data in m is assumed to be invertable
1228

13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
29+
cacheSolve <- function(m, ...) {
30+
## Return a matrix that is the inverse of 'x'
31+
cachedInverse <- m$getInverse()
32+
33+
if(!is.null(m))
34+
{
35+
message("getting cached inverse")
36+
return(cachedInverse)
37+
}
38+
matrixData <- m$get()
39+
40+
cachedInverse <- solve(matrixData)
41+
42+
m$setinverse(cachedInverse)
43+
cachedInverse
1544
}

0 commit comments

Comments
 (0)