From 10d001b383af3ff53ec7c2ee2ef4bb8ce5ae920c Mon Sep 17 00:00:00 2001 From: Mohammad Asadi <89099584+MuhammadAsadi95@users.noreply.github.com> Date: Tue, 5 Oct 2021 04:14:07 -0700 Subject: [PATCH] Update cachematrix.R --- cachematrix.R | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..a0b716adf3c 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -2,14 +2,45 @@ ## functions do ## Write a short comment describing this function +##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 +##(there are also alternatives to matrix inversion that we will not discuss here). makeCacheMatrix <- function(x = matrix()) { +#This function creates a special "matrix" object that can cache its inverse. + +makeVector <- function(x = matrix()) { + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinv <- function(inv) m <<- inv + getinv <- function() m + list(set = set, get = get, + setinv = setinv, + getinv = getinv) } +#This function computes the inverse of the special "matrix" returned by makeCacheMatrix above. +#If the inverse has already been calculated (and the matrix has not changed), +#then the cachesolve should retrieve the inverse from the cache. ## Write a short comment describing this function cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' } +cachemean <- function(x, ...) { + m <- x$getinv() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setinv(m) + m +}