Skip to content

Commit 24b77d1

Browse files
committed
initial commit
1 parent 7f657dd commit 24b77d1

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

cachematrix.R

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,52 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## These functions cache a given matrix inversion to conserve
2+
## repeating the computationally complex inversion calculation.
3+
## For the given matrix it will only be calculated once.
34

4-
## Write a short comment describing this function
55

6+
## makeCacheMatrix provides the functions to get and set the matrix
7+
## as well as get and set the inverse
68
makeCacheMatrix <- function(x = matrix()) {
7-
9+
m <- NULL
10+
set <- function(y) {
11+
x <<- y
12+
m <<- NULL
13+
}
14+
get <- function() x
15+
setinverse <- function(inverse) m <<- inverse
16+
getinverse <- function() m
17+
list(set = set, get = get,
18+
setinverse = setinverse,
19+
getinverse = getinverse)
820
}
921

1022

11-
## Write a short comment describing this function
12-
13-
cacheSolve <- function(x, ...) {
23+
## cacheinverse calculates the matrix inverse if it is not cached
24+
## and reads the cache if it is available intead of calculating
25+
cacheinverse <- function(x, ...) {
1426
## Return a matrix that is the inverse of 'x'
27+
m <- x$getinverse()
28+
if(!is.null(m)) {
29+
message("getting cached data")
30+
return(m)
31+
}
32+
data <- x$get()
33+
m <- solve(data, ...)
34+
x$setinverse(m)
35+
m
1536
}
37+
38+
## test console session
39+
##
40+
# > x <- rbind(c(4,7),c(2,6))
41+
# > a <- makeCacheMatrix(x)
42+
# > cacheinverse(a)
43+
# [,1] [,2]
44+
# [1,] 0.6 -0.7
45+
# [2,] -0.2 0.4
46+
# > cacheinverse(a)
47+
# getting cached data
48+
# [,1] [,2]
49+
# [1,] 0.6 -0.7
50+
# [2,] -0.2 0.4
51+
# >
52+

0 commit comments

Comments
 (0)