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

Skip to content

Commit afdec19

Browse files
author
David J. Weller-Fahy
committed
Add matrix caching/solving functions.
1 parent 7f657dd commit afdec19

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

cachematrix.R

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
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+
## Create method of storing matrices and calculating their inversions that only
2+
## calculates the inversion for a given matrix once.
53

4+
## Cached matrix data type (functions to store/retrieve matrix/inversion).
65
makeCacheMatrix <- function(x = matrix()) {
7-
6+
m = NULL
7+
set <- function(y) {
8+
x <<- y
9+
m <<- NULL
10+
}
11+
get <- function() x
12+
setinverse <- function(inverse) m <<- inverse
13+
getinverse <- function() m
14+
list(set = set, get = get,
15+
setinverse = setinverse,
16+
getinverse = getinverse)
817
}
918

10-
11-
## Write a short comment describing this function
12-
13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
19+
## Calculate inversion of matrix stored in cached matrix data type if needed.
20+
cacheSolve <- function(x) {
21+
## Return a matrix that is the inverse of 'x' (calculate if necessary).
22+
i <- x$getinverse()
23+
if(!is.null(i)) {
24+
message('getting cached data')
25+
return(i)
26+
}
27+
data <- x$get()
28+
i <- solve(data)
29+
x$setinverse(i)
30+
i
1531
}

0 commit comments

Comments
 (0)