Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions cachematrix.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
## Put comments here that give an overall description of what your
## functions do

## Write a short comment describing this function

makeCacheMatrix <- function(x = matrix()) {

# This function creates a special "matrix" object that can cache its inverse
makeCacheMatrix <- function() {
mat <- NULL
inv_cache <- NULL

setMatrix <- function(x) {
mat <<- x
inv_cache <<- NULL
}

getMatrix <- function() {
mat
}
# Create a special "matrix" object
special_matrix <- makeCacheMatrix()

# Set the matrix
special_matrix$setMatrix(matrix(c(1, 2, 3, 4), 2, 2))

# Compute and cache the inverse
inverse_matrix <- cacheSolve(special_matrix)
print(inverse_matrix)

# Retrieve the cached inverse
cached_inverse <- cacheSolve(special_matrix)
print(cached_inverse)

getInverse <- function() {
if (is.null(inv_cache)) {
inv_cache <<- solve(mat)
}
inv_cache
}

list(setMatrix = setMatrix, getInverse = getInverse)
}


## Write a short comment describing this function


# This function computes the inverse of the special "matrix" returned by makeCacheMatrix
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv_cache <- x$getInverse()
inv_cache
}