diff --git a/.RData b/.RData new file mode 100644 index 00000000000..035319804d8 Binary files /dev/null and b/.RData differ diff --git a/.Rhistory b/.Rhistory new file mode 100644 index 00000000000..86a2c5afbaa --- /dev/null +++ b/.Rhistory @@ -0,0 +1,27 @@ +source("cachematrix.R") +q() +source("cachematrix.R") +a <- makeCacheMatrix(matrix(1:4,2)) +a$get() +a$getInverse() +a$set(matrix(5:8,2)) +a$get() +cacheSolve(a) +cacheSolve(a) +a$getInverse() +b = a$getInverse() +a$get() %*% b +q() +q() +source("cachematrix.R") +a <- makeCacheMatrix(matrix(1:4,2)) +a$get() +a$getInverse() +a$set(matrix(5:8,2)) +a$get() +cacheSolve(a) +cacheSolve(a) +a$getInverse() +b = a$getInverse() +a$get() %*% b +q() diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..53bb07f753d 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,39 @@ ## 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", which is really a list containing +## a function to +## 1. set the value of the matrix +## 2. get the value of the matrix +## 3. set the value of the inverse of the matrix +## 4. get the value of the inverse of the matrix makeCacheMatrix <- function(x = matrix()) { - + i <- NULL + set <- function(y) { + x <<- y + i <<- NULL + } + get <- function() x + setInverse <- function(solve) i <<- solve + getInverse <- function() i + list(set = set, get = get, + setInverse = setInverse, + getInverse = getInverse) } -## Write a short comment describing this function - +## This function calculate the inverse of the special "matrix" created with +## makeCacheMatrix function. However, it first check to see if the inverse +## of the matrix has already been calculated. 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 }