|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## A set of functions to create object to cache matrix inversion and to compute matrix inversion |
| 2 | +## makeCacheMatrix - creates special "matrix" pbject |
| 3 | +## cacheSolve - calculates inversion of th eobject created by makeCacheMatrix. |
| 4 | +## This function will also cache the result and returns cached calculations |
| 5 | +## if the inverse has already been calculated (and the matrix has not changed) |
3 | 6 |
|
4 |
| -## Write a short comment describing this function |
| 7 | +# makeCacheMatrix creates a special "vector", which is really a list containing a function to |
| 8 | +# set the value of the matric |
| 9 | +# get the value of the matrix |
| 10 | +# set the value of the inverted matrix |
| 11 | +# get the value of the inverted matrix |
5 | 12 |
|
6 | 13 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 14 | + inverse <- NULL |
| 15 | + set <- function(y) { |
| 16 | + x <<- y |
| 17 | + inverse <<- NULL |
| 18 | + } |
| 19 | + |
| 20 | + get <- function() x |
| 21 | + |
| 22 | + setinverse <- function(i) inverse <<- i |
| 23 | + getinverse <- function() inverse |
| 24 | + |
| 25 | + list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) |
8 | 26 | }
|
9 | 27 |
|
10 | 28 |
|
11 |
| -## Write a short comment describing this function |
| 29 | +# cacheSolve receive a special "cacheMatrix" object, and then returns |
| 30 | +# cached inverted matrix if it was cached or comuputes new one, caches it and returns the result |
12 | 31 |
|
13 | 32 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 33 | + inverse <- x$getinverse() |
| 34 | + if(!is.null(inverse)) { |
| 35 | + message("getting cached data") |
| 36 | + return(inverse) |
| 37 | + } |
| 38 | + |
| 39 | + matrix <- x$get() |
| 40 | + inverse <- solve(matrix) |
| 41 | + x$setinverse(inverse) |
| 42 | + inverse |
15 | 43 | }
|
| 44 | + |
| 45 | + |
| 46 | + |
0 commit comments