From 0857bfdb430718ba6da2c379fd307ec05f981827 Mon Sep 17 00:00:00 2001 From: NilsKa Date: Mon, 21 Apr 2014 19:51:45 +0200 Subject: [PATCH] Update cachematrix.R --- cachematrix.R | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..fc982639851 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,35 @@ -## Put comments here that give an overall description of what your -## functions do +## MakeCacheMatrix is a function that creates a matrix and caches its inverse. +## CacheSolve is a function that either creates the inverse of a matrix +## or just recalls it in case it has been cached before. -## Write a short comment describing this function -makeCacheMatrix <- function(x = matrix()) { +## makeCacheMatrix is a function that creates a Matrix in the same way the +## matrix() argument does: content = data; nrow == nrow; ncol == ncol; +## argument of matrix(). It will save itself in the object 'original +## Matrix'. Furthermore, it will cache its inverse and save it under +## 'CacheMatrix'. + +makeCacheMatrix <- function(x = matrix()) { + originalMatrix <<- x + CacheMatrix <<- solve(x) + message("Matrix has been cached...") } -## Write a short comment describing this function +## cacheSolve is a function that computes the inverse of a matrix. Thereby, +## it checks whether this Matrix has already been cached by checking the +## saved objects of makeCacheMatrix. -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' +cacheSolve <- function(x = matrix()) { + ## Return a matrix that is the inverse of 'x' + if(exists("CacheMatrix") == TRUE + & all(x == originalMatrix) == TRUE) { + message("getting cached data") + return(CacheMatrix) + } else { + output <- solve(x) + message("calculating...") + return(output) + } }