|
1 | | - |
| 1 | +## This function creates a matrix object that can cache its inverse |
| 2 | +makeCacheMatrix <- function(x = matrix()){ |
| 3 | +inv <- NULL |
| 4 | +set <- function(y) { |
| 5 | +x <<- y |
| 6 | +inv <<- NULL |
| 7 | +} |
| 8 | +get <- function() x |
| 9 | +setInverse <- function(inverse) inv <<- inverse |
| 10 | +getInverse <- function() inv |
| 11 | +list(set = set, get = get, |
| 12 | +setInverse = setInverse, |
| 13 | +getInverse = getInverse) |
| 14 | +} |
| 15 | +## This function computes the inverse of the matrix created by the function |
| 16 | +##above |
| 17 | +cacheSolve <- function(x, ...){ |
| 18 | +## Return a matrix that is the inverse of x |
| 19 | +inv <- x$getInverse() |
| 20 | +if (!is.null(inv)){ |
| 21 | +message("getting cached data") |
| 22 | +return(inv) |
| 23 | +} |
| 24 | +mat <- x$get() |
| 25 | +inv <- solve(mat, ...) |
| 26 | +x$setInverse(inv) |
| 27 | +inv |
| 28 | +} |
| 29 | +gwd() |
| 30 | +getwd() |
| 31 | +setwd("C:/Users/LEITON/ProgrammingAssignment2/ProgrammingAssignment2") |
| 32 | +## This function creates a matrix object that can cache its inverse |
| 33 | +makeCacheMatrix <- function(x = matrix()){ |
| 34 | +inv <- NULL |
| 35 | +set <- function(y) { |
| 36 | +x <<- y |
| 37 | +inv <<- NULL |
| 38 | +} |
| 39 | +get <- function() x |
| 40 | +setInverse <- function(inverse) inv <<- inverse |
| 41 | +getInverse <- function() inv |
| 42 | +list(set = set, get = get, |
| 43 | +setInverse = setInverse, |
| 44 | +getInverse = getInverse) |
| 45 | +} |
| 46 | +## This function computes the inverse of the matrix created by the function |
| 47 | +##above |
| 48 | +cacheSolve <- function(x, ...){ |
| 49 | +## Return a matrix that is the inverse of x |
| 50 | +inv <- x$getInverse() |
| 51 | +if (!is.null(inv)){ |
| 52 | +message("getting cached data") |
| 53 | +return(inv) |
| 54 | +} |
| 55 | +mat <- x$get() |
| 56 | +inv <- solve(mat, ...) |
| 57 | +x$setInverse(inv) |
| 58 | +inv |
| 59 | +} |
| 60 | +ex_matrix <- makeCacheMatrix(matrix(2:6),2,2) |
| 61 | +ex_matrix <- makeCacheMatrix(matrix(2:6),2,2)) |
| 62 | +ex_matrix <- makeCacheMatrix(matrix(2:6,2,2)) |
| 63 | +ex_matrix |
| 64 | +ex_matrix$get() |
| 65 | +ex_matrix <- makeCacheMatrix(matrix(3:6,2,2)) |
| 66 | +ex_matrix$get() |
| 67 | +ex_matrix$getInverse() |
| 68 | +cacheSolve(ex_matrix) |
| 69 | +cacheSolve(ex_matrix) |
| 70 | +ex_matrix$getInverse() |
0 commit comments