From 4273874b1ef55c626746bd30f237d1b34e29b04d Mon Sep 17 00:00:00 2001 From: knokname Date: Thu, 24 Apr 2014 22:26:43 -1000 Subject: [PATCH] adding R Programming peer assignment --- .Rhistory | 36 ++++++++++++++++++++++++++++++++++++ cachematrix.R | 30 +++++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 .Rhistory diff --git a/.Rhistory b/.Rhistory new file mode 100644 index 00000000000..f9a795d1fa8 --- /dev/null +++ b/.Rhistory @@ -0,0 +1,36 @@ +makeCacheMatrix <- function(x = matrix()) { +m <- NULL +set <- function(y){ +x <<- y +m <<- NULL +} +get <- function() x +setinverse <- function(solve) m <<- solve +getinverse <- function() m +list(set = set, get = get, +setinverse = setinverse, +getinverse = getinverse) +} +a <- makeCacheMAtrix(matrix(1:4,2)) +a <- makeCacheMatrix(matrix(1:4,2)) +a$get() +a$getinverse() +cacheSolve <- function(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,...) ## returns inverse of matrix data <- x +x$setinverse(m) +m +} +a$set +a$set(matrix(5:8,2)) +a$get() +cachesolve(a) +cacheSolve(a) +cacheSolve(a) +a$getinverse() diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..731dde81a71 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,35 @@ -## Put comments here that give an overall description of what your -## functions do +## Cache matrix inversion and allows look up in cache if +## matrix was previously inverted -## Write a short comment describing this function +## This function creates a special "matrix" object that can cache its inverse makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y){ + x <<- y + m <<- NULL + } + get <- function() x + setinverse <- function(solve) m <<- solve + getinverse <- function() m + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } -## Write a short comment describing this function +## This function computes the special "matrix" returned in makeCacheMatrix; +## if the inverse has already been calculated, this function retrieves from the cache cacheSolve <- function(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,...) ## returns inverse of matrix data <- x + x$setinverse(m) + m }