From bf8a8f300c53114d3735642a1034b01adc66763d Mon Sep 17 00:00:00 2001 From: Pradeep Gowda Date: Sun, 26 Apr 2015 12:29:46 -0400 Subject: [PATCH 1/2] First attempt at solving the assignment problem --- cachematrix.R | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..326511dd4b5 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,49 @@ -## Put comments here that give an overall description of what your -## functions do +# Caching the Inverse of a Matrix -## Write a short comment describing this function +## Matrix inversion is usually a costly computation and there may be +## some benefit to caching the inverse of a matrix rather than +## computing it repeatedly. Here are a pair of functions that cache +## the inverse of a matrix. + + +## makeCacheMatrix creates a special "matrix" object that can cache +## its inverse. makeCacheMatrix <- function(x = matrix()) { + matInverse <- NULL + set <- function (y) { + x <<- y + matInverse <<- NULL + } + + get <- function() x + setinverse <- function (mi) matInverse <<- mi + getinverse <- function () matInverse + + list (set = set, get = get, + setinverse = setinverse, + getinverse = getinverse + ) } -## Write a short comment describing this function + +## cacheSolve computes the inverse of the special "matrix" returned by +## makeCacheMatrix above. If the inverse has already been calculated +## (and the matrix has not changed), then cacheSolve should retrieve +## the inverse from the cache. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + matInverse <- x$getinverse() + if (!is.null(matInverse)){ + message("getting cached data") + return(matInverse) + } + + data <- x$get() + matInverse <- solve(data, ...) + x$setinverse(matInverse) + matInverse } From 1fb43a0c5a8543f813ae3d9e99b513dde0cfa424 Mon Sep 17 00:00:00 2001 From: Pradeep Gowda Date: Sun, 26 Apr 2015 12:40:30 -0400 Subject: [PATCH 2/2] verfied the solution; formatting --- cachematrix.R | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 326511dd4b5..2ff50da47ab 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -6,7 +6,7 @@ ## the inverse of a matrix. -## makeCacheMatrix creates a special "matrix" object that can cache +## makeCacheMatrix - create a special "matrix" object that can cache ## its inverse. makeCacheMatrix <- function(x = matrix()) { @@ -29,13 +29,14 @@ makeCacheMatrix <- function(x = matrix()) { -## cacheSolve computes the inverse of the special "matrix" returned by -## makeCacheMatrix above. If the inverse has already been calculated -## (and the matrix has not changed), then cacheSolve should retrieve -## the inverse from the cache. +## cacheSolve - compute the inverse of the special "matrix" returned +## by makeCacheMatrix above. If the inverse has already been +## calculated (and the matrix has not changed), then cacheSolve should +## retrieve the inverse from the cache. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + matInverse <- x$getinverse() if (!is.null(matInverse)){ message("getting cached data")