Skip to content

Commit 4adbdfc

Browse files
committed
Functions to cache a inverse matrix calculation
1 parent 7f657dd commit 4adbdfc

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

cachematrix.R

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
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+
## Pair of functions to cache a matrix inverse calculation
2+
# example of use:
3+
# m = matrix(rnorm(100),nrow=10)
4+
# m = makeCacheMatrix(m)
5+
# mi = cacheSolve(m)
56

7+
## Creates a special "matrix" object that can cache its 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-
23+
## Computes the inverse of the special "matrix" returned by makeCacheMatrix
24+
## f the inverse has already been calculated (and the matrix has not changed),
25+
## then cacheSolve should retrieve the inverse from the cache.
1326
cacheSolve <- function(x, ...) {
14-
## 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
}

0 commit comments

Comments
 (0)