Skip to content

Commit

Permalink
My solution for the Programming Assignment 2
Browse files Browse the repository at this point in the history
  • Loading branch information
cdromain committed Sep 26, 2015
1 parent 7f657dd commit 41ab08f
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions cachematrix.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
## Put comments here that give an overall description of what your
## functions do
## R Programming - Week 3 - Programming Assignment 2
##
## This code file contains 2 functions, makeCacheMatrix and cacheSolve,
## which are meant to be used the following way :
##
## 1. create a matrix object, for example : x <- matrix(rnorm(100, 1), 10, 10)
## 2. list <- makeCacheMatrix(x)
## 3. cacheSolve(list)

## Write a short comment describing this function
## The makeCacheMatrix function creates & returns a special list containing 4 functions
##
## 1. set : sets the content of the matrix which we'll calculate the inverse of
## 2. get : gets the content of the matrix
## 3. setInv : sets the cached value of the matrix inverse
## 4. getInv : gets the cached value of the matrix inverse

makeCacheMatrix <- function(x = matrix()) {

i <- NULL # inverse variable
set <- function(y) {
x <<- y
i <<- NULL
}
get <- function() x
setInv <- function(inv) i <<- inv
getInv <- function() i
list(set = set, get = get,
setInv = setInv,
getInv = getInv)
}


## Write a short comment describing this function
## The cacheSolve function computes and returns a matrix that is the inverse of 'x'
## but only if the inverse has not already been calculated (i.e. i = NULL)
## or if the matrix has changed since the last inverse computation (which sets i as NULL)
## otherwise the function retrieves the cached inverse value using the get function

cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
i <- x$getInv()
if(!is.null(i)) { # this tests if the inverse i has already been computed and if matrix has changed
message("Cached data available and the matrix has not changed. Getting cached inverse.")
return(i)
}
else message("No cached data available or the matrix has changed. Calculating the inverse and caching it.")
data <- x$get()
i <- solve(data, ...)
x$setInv(i)
i
}

0 comments on commit 41ab08f

Please sign in to comment.