diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..e1afa15df04 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,52 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { - +# Creates a special matrix object that can cache its inverse +# Argument parameter: Matrix, Output: List +makeCacheMatrix <- function(x = matrix()){ + # Initialize the inverse value + m <- NULL + + # Method to set the matrix + set <- function(y){ + x <<- y + m <<- NULL + } + + # Method to get the matrix + get <- function() x + + # Method to set the inverse of the matrix + setinverse <- function(inverse) m <<- inverse + + # Method to get the inverse of the matrix + getinverse <- function() m + + # Output list + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } -## Write a short comment describing this function - -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' +# To compute the inverse of the special matrix +# Argument parameter: Matrix, Output: Inverse Matrix +cacheSolve <- function(x, ...){ + # Initialize a matrix that is the inverse of x matrix + m <- x$getinverse() + + # Return a matrix if it is the inverse of x matrix + if(!is.null(m)) { + message("getting cached data") + return(m) + } + + # Get the matrix from the object + data <- x$get() + + # Method to solve the inverse using matrix multiplication + m <- solve(data, ...) + + # Set the inverse of inverse matrix + x$setinverse(m) + + # Return the matrix + m }