From 8d32353a7452d8c5240e4cd7d5be401c868b61c2 Mon Sep 17 00:00:00 2001 From: Kalimdor Date: Mon, 21 Apr 2014 09:13:04 +0200 Subject: [PATCH 1/2] 2nd programming assignment --- cachematrix.R | 35 ++++++++++++++++++++++++++--------- test.R | 6 ++++++ 2 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 test.R diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..72da4c9a531 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,32 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## Functions for calculating and caching the inverse of a matrix +## Makes a matrix object able to store a cache of its inverse makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinverse <- function(inverse) m <<- inverse + getinverse <- function() m + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } - -## Write a short comment describing this function - +## Calculates the inverse of a matrix enhanced with makeCacheMatrix and caches it. +## It returns the cached version if it was already computed. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + m <- x$getinverse() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- solve(data) + x$setinverse(m) + m } + diff --git a/test.R b/test.R new file mode 100644 index 00000000000..31a60a4a25e --- /dev/null +++ b/test.R @@ -0,0 +1,6 @@ +source("cachematrix.R") + +mdat <- matrix(c(1,2,3, 11,12,13,55,11,-3), nrow = 3, ncol = 3); +m <- makeCacheMatrix(mdat); +c <- cacheSolve(m); +d <- cacheSolve(m); From f2342ca4eeb00b8e761c17da1938a3fa1851d4fe Mon Sep 17 00:00:00 2001 From: Kalimdor Date: Mon, 21 Apr 2014 09:16:03 +0200 Subject: [PATCH 2/2] 2nd programming assignment --- cachematrix.R | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 72da4c9a531..6db78578800 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -2,14 +2,14 @@ ## Makes a matrix object able to store a cache of its inverse makeCacheMatrix <- function(x = matrix()) { - m <- NULL + i <- NULL set <- function(y) { x <<- y - m <<- NULL + i <<- NULL } get <- function() x - setinverse <- function(inverse) m <<- inverse - getinverse <- function() m + setinverse <- function(inverse) i <<- inverse + getinverse <- function() i list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) @@ -19,14 +19,14 @@ makeCacheMatrix <- function(x = matrix()) { ## It returns the cached version if it was already computed. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' - m <- x$getinverse() - if(!is.null(m)) { + i <- x$getinverse() + if(!is.null(i)) { message("getting cached data") - return(m) + return(i) } data <- x$get() - m <- solve(data) - x$setinverse(m) - m + i <- solve(data) + x$setinverse(i) + i }