|
1 | 1 | ## Put comments here that give an overall description of what your |
2 | 2 | ## functions do |
| 3 | +## function makeCacheMatrix() creates a list of functions |
| 4 | +## that stored the original matrix and the invese matrix if it has been calculated |
| 5 | +## function cacheSolve() calculates the inverse matrix |
3 | 6 |
|
4 | 7 | ## Write a short comment describing this function |
| 8 | +## this function returns a list which: |
| 9 | +## set the value of the original matrix |
| 10 | +## get the value of the original matrix |
| 11 | +## set the value of the inverse matrix |
| 12 | +## get the value of the inverse matrix |
5 | 13 |
|
6 | 14 | makeCacheMatrix <- function(x = matrix()) { |
7 | | - |
| 15 | + inversem <- NULL |
| 16 | + |
| 17 | + set <- function(y) { |
| 18 | + x <<- y |
| 19 | + inversem <<- NULL |
| 20 | + } |
| 21 | + |
| 22 | + get <- function() x |
| 23 | + |
| 24 | + calinverse <- function(inverse) inversem <<- inverse |
| 25 | + |
| 26 | + getinverse <- function() inversem |
| 27 | + |
| 28 | + list(set = set, get = get, |
| 29 | + calinverse = calinverse, |
| 30 | + getinverse = getinverse) |
8 | 31 | } |
9 | 32 |
|
10 | 33 |
|
11 | 34 | ## Write a short comment describing this function |
| 35 | +## calculate the inverse matrix and cache it |
| 36 | +## and it assumes that matrix x is invertible |
12 | 37 |
|
13 | 38 | cacheSolve <- function(x, ...) { |
14 | 39 | ## Return a matrix that is the inverse of 'x' |
| 40 | + inversem <- x$getinverse() |
| 41 | + if (!is.null(inversem)) { |
| 42 | + message("getting cached data") |
| 43 | + return(inversem) |
| 44 | + } |
| 45 | + data <- x$get() |
| 46 | + inverse <- solve(data) |
| 47 | + inversem <- x$calinverse(inverse) |
| 48 | + inversem |
15 | 49 | } |
0 commit comments