Skip to content
This repository was archived by the owner on Feb 1, 2024. It is now read-only.

Commit 1f9805e

Browse files
committed
Assignment completed.
1 parent 7f657dd commit 1f9805e

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

cachematrix.R

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Matrix inversion is usually a costly computation and
2+
## there may be some benefit to caching the inverse of a matrix
3+
## rather than compute it repeatedly.
4+
##
5+
## The functions below cache the inverse of a matrix.
36

4-
## Write a short comment describing this function
7+
# AUTHOR: Fabio Correa
8+
9+
# COURSE: R Programming (rprog-032)
10+
# TASK : Programming Assignment 2 - Caching the Inverse of a Matrix
511

6-
makeCacheMatrix <- function(x = matrix()) {
12+
## Create special matrix object that caches its inverse.
713

14+
makeCacheMatrix <- function(x = matrix()) {
15+
x.inverse <- NULL
16+
set <- function(x.new) {
17+
x <<- x.new
18+
x.inverse <<- NULL
19+
}
20+
get <- function() x
21+
setinverse <- function(inv) x.inverse <<- inv
22+
getinverse <- function() x.inverse
23+
list(set = set, get = get, setinverse = setinverse, getinverse = getinverse)
824
}
925

1026

11-
## Write a short comment describing this function
27+
## Compute the inverse of the special matrix returned by
28+
## 'makeCacheMatrix', unless it exists in cache, where it
29+
## is retrieved from.
1230

1331
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
32+
## Return a matrix that is the inverse of 'x'
33+
x.inverse <- x$getinverse()
34+
if(!is.null(x.inverse)) {
35+
message("Getting cached data")
36+
return(x.inverse)
37+
}
38+
data <- x$get()
39+
x.inverse <- solve(data, ...)
40+
x$setinverse(x.inverse)
41+
x.inverse
1542
}

0 commit comments

Comments
 (0)