From a54fefb66fb7b927de3a25d0206a7fdfb5af3bdb Mon Sep 17 00:00:00 2001 From: Markmydata Date: Mon, 13 May 2019 15:20:27 +0200 Subject: [PATCH] My answer to the second programming assignment version #1 --- cachematrix.R | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..173ea9bb5ba 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,33 @@ -## Put comments here that give an overall description of what your -## functions do +## These functions combined, will be able to return an inverse of a matrix. +## The function is written so that, should a matrix have remained the same, it will returned it's previously cached result, rather than computing the inverse again, +## making the solution more efficient. -## Write a short comment describing this function +## makeCachematrix creates a 'special' matrix out of a simple matrix and makes it able to store the inverse in the cached memory makeCacheMatrix <- function(x = matrix()) { - +m <- NULL +set <- function(y){ + x <<- y + m <<- NULL +} +get <- function() x +setsolve <- function(solve) m <<- solve +getsolve <- function() m +list(set = set, get = get, setsolve = setsolve, getsolve = getsolve) } -## Write a short comment describing this function +## cacheSolve checks to see if the special matrix matches the special matrix from the previously cached inverse, and returns the cached inverse if true. +## Should the special matrix have been altered, it computes and returns the appropriate inverse. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + m <- x$getsolve() + if(!is.null(m)){ + message("getting cached data") + return(m) + } + data <- x$get() + m <- solve(data,...) + x$setsolve(m) + m }