|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## The following functions are used to compute the inverse matrix of an input matrix and cache its result. |
| 2 | +## If the inverse matrix of the input matrix has already been computed, a cached result is returned. |
| 3 | +## Otherwise, the inverse matrix is computed. |
| 4 | +## Test, create matrix: mat <- matrix(c(51, 60, 7, 8), nrow=2, ncol=2) |
| 5 | +## |
3 | 6 |
|
4 |
| -## Write a short comment describing this function |
| 7 | + |
| 8 | +## makeCacheMatrix: Creates a a special object "matrix" that stores a matrix, computes its inverse using the solve function |
| 9 | +## and caches the result. |
| 10 | +##Test, amekCacheMatrix: a<-makeCacheMatrix(mat) |
5 | 11 |
|
6 | 12 | makeCacheMatrix <- function(x = matrix()) {
|
| 13 | + |
| 14 | + m <- NULL |
| 15 | + set <- function(y) { |
| 16 | + x <<- y |
| 17 | + m <<- NULL |
| 18 | + } |
| 19 | + get <- function() x |
| 20 | + setInvMatrix <- function(solve) m <<- solve |
| 21 | + getInvMatrix <- function() m |
| 22 | + list(set = set, get = get, |
| 23 | + setInvMatrix = setInvMatrix, |
| 24 | + getInvMatrix = getInvMatrix) |
7 | 25 |
|
8 | 26 | }
|
9 | 27 |
|
10 | 28 |
|
11 |
| -## Write a short comment describing this function |
| 29 | +## cacheSolve: Calculates the inverse matrix using the solve function with the matrix object |
| 30 | +## created in the makeCacheMatrix function. |
| 31 | +##Test, get cachec value or compute: cacheSolve(a) |
12 | 32 |
|
13 | 33 | cacheSolve <- function(x, ...) {
|
14 | 34 | ## Return a matrix that is the inverse of 'x'
|
| 35 | + |
| 36 | + m <- x$getInvMatrix() |
| 37 | + if(!is.null(m)) { |
| 38 | + message("getting cached data") |
| 39 | + return(m) |
| 40 | + } |
| 41 | + data <- x$get() |
| 42 | + m <- solve(data, ...) |
| 43 | + x$setInvMatrix(m) |
| 44 | + m |
15 | 45 | }
|
0 commit comments