Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rdpeng/ProgrammingAssignment2
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: Simberific/ProgrammingAssignment2
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Checking mergeability… Don’t worry, you can still create the pull request.
  • 1 commit
  • 1 file changed
  • 1 contributor

Commits on Jul 28, 2014

  1. Adding cachematrix.R

    simberific committed Jul 28, 2014
    Copy the full SHA
    1c03031 View commit details
Showing with 31 additions and 6 deletions.
  1. +31 −6 cachematrix.R
37 changes: 31 additions & 6 deletions cachematrix.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
## Put comments here that give an overall description of what your
## functions do
## Below are two functions, makeCacheMatrix and cacheSolve, which together allow for caching of the inverse of a matrix, so that you don't have to solve for the inverse (which is expensive) if you have already computed this.

## Write a short comment describing this function

makeCacheMatrix <- function(x = matrix()) {
## makeCacheMatrix creates a matrix object which has functions allowing the user to get the value of the matrix itself, set it, get the "cached" inverse of the matrix, and set the cached inverse of the matrix.

makeCacheMatrix <- function(x = matrix()) {
inv <- NULL ## Sets inverse to NULL when you first make the cached entry

## Define a set function which sets the value of the vector and re-sets cached inverse to NULL (since you've changed the vector, the previous inverse is no longer valid)
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x ## get function returns the value of the vector
setinverse <- function(i) inv <<- i ## Sets the cached value of the inverse of x to i
getinverse <- function() inv ## Returns the value of the cached inverse


## Return the four functions as elements of a list
list(set = set, get = get, setinverse = setinverse, getinverse = getinverse)

}


## Write a short comment describing this function
## cacheSolve checks whether the matrix's inverse has already been computed, and if so, retrieves it; if not, computes it, caches it to the object as inv, and returns it.

cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getinverse()
if (!is.null(inv)) {
## Then you just return the inverse from cache
message("getting cached data")
return(inv)
}
## Else, you compute the inverse
data <- x$get()
newInv <- solve(data, ...)
x$setinverse(newInv) ## Set x's inverse to the newly computed inverse
newInv

}