8000 Commit #1 · audreyuu/ProgrammingAssignment2@9e40d55 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e40d55

Browse files
author
avy
committed
Commit rdpeng#1
1 parent 7f657dd commit 9e40d55

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

cachematrix.R

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,55 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
##- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2+
## R Programming on Coursera
3+
## Programming Assignment #2: Create two functions
4+
##
5+
## (1) makeCacheMatrix
6+
## This function creates a special "matrix" object that can cache its inverse.
7+
## For this assignment, assume that the matrix supplied is always invertible.
8+
## (2) cacheSolve:
9+
## This function computes the inverse of the special "matrix" returned by makeCacheMatrix above.
10+
## If the inverse has already been calculated (and the matrix has not changed), then cacheSolve retrieves the inverse from the cache.
11+
##- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
312

4-
## Write a short comment describing this function
513

6-
makeCacheMatrix <- function(x = matrix()) {
14+
## Function #1: makeCacheMatrix
15+
## makeCacheMatrix takes an invertible, square matrix and returns a list of four functions that:
16+
## (1) set the value of the matrix (2) get the value of the matrix (3) set the value of the inverse (4) get the value of the inverse
717

18+
makeCacheMatrix <- function(x = matrix()) {
19+
if(ifelse(nrow(x) == ncol(x), ifelse(det(x) != 0, TRUE, FALSE), FALSE)) { # check if the matrix square and invertible
20+
inv <- NULL
21+
set <- function(y) {
22+
x <<- y
23+
inv <<- NULL
24+
}
25+
get <- function() x
26+
setInverse <- function(solve) inv <<- solve
27+
getInverse <- function() inv
28+
list(set = set, get = get,
29+
setInverse = setInverse,
30+
getInverse = getInverse)
31+
}
32+
else { # if matrix not square and invertible, then throw an error
33+
message("[makeCacheMatrix error]: 'x' must be a square, invertible matrix")
34+
}
835
}
936

1037

11-
## Write a short comment describing this function
38+
39+
40+
## FUNCTION #2: cacheSolve
41+
## cacheSolve takes the list created by makeCacheMatrix and returns the inverse of the matrix represented in that list.
42+
## If the inverse has already been calculated, cacheSolve pulls the inverse from the cache.
43+
## Otherwise, cacheSolve calculates and sets the value of the inverse in the cache.
1244

1345
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
15-
}
46+
inv <- x$getInverse()
47+
if(!is.null(inv)) {
48+
message("getting cached data")
49+
return(inv)
50+
}
51+
data <- x$get()
52+
inv <- solve(data, ...)
53+
x$setInverse(inv)
54+
inv
55+
}

0 commit comments

Comments
 (0)
0