From 8d152d949100d3453de4c8092131791100959151 Mon Sep 17 00:00:00 2001 From: Peter Nowotnick Date: Sat, 12 Apr 2014 19:47:55 +0200 Subject: [PATCH 1/2] completed homework assignment --- .Rhistory | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++ cachematrix.R | 40 ++++++++++--- 2 files changed, 186 insertions(+), 7 deletions(-) create mode 100644 .Rhistory diff --git a/.Rhistory b/.Rhistory new file mode 100644 index 00000000000..965aad08b18 --- /dev/null +++ b/.Rhistory @@ -0,0 +1,153 @@ +## Put comments here that give an overall description of what your +## functions do +## Write a short comment describing this function +makeCacheMatrix <- function(x = matrix()) { +i <- NULL +set <- function(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) +} +## Return a matrix that is the inverse of 'x' +## Calculate and store the inverse if it +## hadn't been calculated before +## otherwise use the stored result +cacheSolve <- function(x, ...) { +i <- x$getinverse() +if(!is.null(i)) { +message("getting cached data") +return(i) +} +data <- x$get() +i <- solve(data) +x$setinverse(i) +i +} +} +## Put comments here that give an overall description of what your +## functions do +## Write a short comment describing this function +makeCacheMatrix <- function(x = matrix()) { +i <- NULL +set <- function(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) +} +## Return a matrix that is the inverse of 'x' +## Calculate and store the inverse if it +## hadn't been calculated before +## otherwise use the stored result +cacheSolve <- function(x, ...) { +i <- x$getinverse() +if(!is.null(i)) { +message("getting cached data") +return(i) +} +data <- x$get() +i <- solve(data) +x$setinverse(i) +i +} +matrix <- makeCacheMatrix() +matrix +matrix <- makeCacheMatrix(c(0,9)) +matrix +matrix.get() +matrix <- makeCacheMatrix(matrix([1, 2, 3, 4], nrows=2) +matrix(1, 2, 3, 4) +matrix(1, 2, 3, 4, nrows=2) +matrix((1, 2, 3, 4) nrows=2) +matrix((1, 2, 3, 4), nrows=2) +matrix([1, 2, 3, 4], nrows=2) +?matrix +matrix(c(1, 2, 3, 4), nrows=2) +matrix(c(1, 2, 3, 4), nrow=2) +matrix(c(1, 2, 3, 4), nrow=2) +m <- makeCacheMatrix(matrix(c(1, 2, 3, 4), nrow=2)) +m +m.get +m.get() +m.get +get(m) +m +m.get() +m.getinverse() +## Put comments here that give an overall description of what your +## functions do +## This function creates a special "matrix" object that can cache its inverse. +makeCacheMatrix <- function(x = matrix()) { +i <- NULL +set <- function(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) +} +## Return a matrix that is the inverse of 'x' +## Calculate and store the inverse if it +## hadn't been calculated before +## otherwise use the stored result +cacheSolve <- function(x, ...) { +i <- x$getinverse() +if(!is.null(i)) { +message("getting cached data") +return(i) +} +data <- x$get() +i <- solve(data) +x$setinverse(i) +i +} +m <- makeCacheMatrix(matrix(c(1, 2, 3, 4), nrow = 2)) +m +## Put comments here that give an overall description of what your +## functions do +## This function creates a special "matrix" object that can cache its inverse. +makeCacheMatrix <- function(x = matrix()) { +i <- NULL +set <- function(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) +} +## Return a matrix that is the inverse of 'x' +## Calculate and store the inverse if it +## hadn't been calculated before +## otherwise use the stored result +cacheSolve <- function(x, ...) { +i <- x$getinverse() +if(!is.null(i)) { +message("getting cached data") +return(i) +} +data <- x$get() +i <- solve(data) +x$setinverse(i) +i +} +m <- makeCacheMatrix(matrix(c(1, 2, 3, 4), nrow = 2)) +m.get() diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..434c6e773df 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,41 @@ ## Put comments here that give an overall description of what your ## functions do -## Write a short comment describing this function - +## This function creates a special "matrix" object that can cache its inverse. makeCacheMatrix <- function(x = matrix()) { - + i <- NULL + set <- function(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 - +## Return a matrix that is the inverse of 'x' +## Calculate and store the inverse if it +## hadn't been calculated before +## otherwise use the stored result cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + i <- x$getinverse() + if(!is.null(i)) { + message("getting cached data") + return(i) + } + data <- x$get() + i <- solve(data) + x$setinverse(i) + i } + +## Test +m <- makeCacheMatrix(matrix(c(1, 2, 3, 4), nrow = 2)) +m$get() +m$getinverse() +cacheSolve(m) +cacheSolve(m) +m$getinverse() From 3cdbabd26e426a3833cb798d4a89eaf0120648f1 Mon Sep 17 00:00:00 2001 From: Peter Nowotnick Date: Sat, 12 Apr 2014 19:53:12 +0200 Subject: [PATCH 2/2] homework assignment --- cachematrix.R | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 434c6e773df..3ed7290245b 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,5 +1,4 @@ -## Put comments here that give an overall description of what your -## functions do +## A pair of functions that allows to cache the inverse of a matrix ## This function creates a special "matrix" object that can cache its inverse. makeCacheMatrix <- function(x = matrix()) { @@ -33,9 +32,9 @@ cacheSolve <- function(x, ...) { } ## Test -m <- makeCacheMatrix(matrix(c(1, 2, 3, 4), nrow = 2)) -m$get() -m$getinverse() -cacheSolve(m) -cacheSolve(m) -m$getinverse() +#m <- makeCacheMatrix(matrix(c(1, 2, 3, 4), nrow = 2)) +#m$get() +#m$getinverse() +#cacheSolve(m) +#cacheSolve(m) +#m$getinverse()