Skip to content
chloeljohnson edited this page Jan 2, 2023 · 2 revisions

<>

##Caching the Inverse of a Matrix: ##Matrix inversion is usually a costly computation and there may be some ##benefit to caching the inverse of a matrix rather than compute it repeatedly. ##Below are two functions that are used to create a special object that ##stores a matrix and caches its inverse.

##This function creates a "matrix" object that can cache its inverse.

makeCacheMatrix <- function(x = matrix()) { inv <- NULL set <- function(y) { x <<- y inv <<- NULL } get <- function() x set.inverse <- function(inverse) inv <<- inverse get.inverse <- function() inv list(set = set, get = get, set.inverse = set.inverse, get.inverse = get.inverse) }

##This function computes the inverse of the special "matrix" created by ##makeCacheMatrix above. If the inverse has already been calculated (and the ##matrix has not changed), then it should retrieve the inverse from the cache.

cacheSolve <- function(x, ...) {

Return a matrix that is the inverse of 'x'

inv <- x$get.inverse() if (!is.null(inv)) { message("getting cached data") return(inv) } mat <- x$get() inv <- solve(mat, ...) x$set.inverse(inv) inv }

Clone this wiki locally