Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions cachematrix.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
## Put comments here that give an overall description of what your
## functions do

## Write a short comment describing this function

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

## This function creates a matrix object that can cache its inverse
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) { # sets the variables x and y
x <<- y
m <<- NULL
}
get <- function() x #gets x
setInverse <- function(inverse) m <<- inverse #sets the inverse to m
getInverse <- function() m #gets m
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}


## Write a short comment describing this function

#This function computes the inverse of the matrix,
#if it has not been computed before or
#retrieves the answer that has been already calculated
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
m <- x$getInverse()#get inverse or NULL if it has not been computed before

if(!is.null(m)) {# if the inverse has been computed before, return that inverse
message("getting cached inverse")
return(m)
}
data <- x$get()
m <- solve(data, ...)#compute inverse if it has not been computed before
x$setInverse(m)
m
}