From 1c3f1aacf9c7af5d1189d67c24948e1501202fa9 Mon Sep 17 00:00:00 2001 From: kolynos Date: Fri, 11 Apr 2014 11:44:36 +0200 Subject: [PATCH 1/2] test --- cachematrix.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..3e146b73af3 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,7 +4,7 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + print("test") } From df8a2edbf3085751ffb7feb2efc577bff3aaf910 Mon Sep 17 00:00:00 2001 From: kolynos Date: Thu, 24 Apr 2014 12:45:25 +0200 Subject: [PATCH 2/2] finished programming assignment --- cachematrix.R | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 3e146b73af3..9bc8b98434f 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,40 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## Creates a special matrix that stores the inverse of regular matrix 'x' makeCacheMatrix <- function(x = matrix()) { - print("test") + i <<- NULL + set <- function(y) + { + if(!identical(x,y)) + { + x<<-y + i<<-NULL + } + } + get <- function()x + setinverse <- function(inverse)i<<-inverse + getinverse <- function() i + list(set=set,get=get,setinverse=setinverse,getinverse=getinverse) } -## Write a short comment describing this function +## Calculates the inverse of special matrix 'x'. Uses cached +## inverse if matrix 'x' has not changed cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + i<-x$getinverse() + if(is.null(i)) + { + m<-x$get() + if(is.na(m[1,1])) + { + message("No matrix to calculate the inverse for!") + } + else + { + message("Calculating new inverse") + x$setinverse(solve(m)) + } + } + return(x$getinverse()) + ## Return a matrix that is the inverse of 'x' }