forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
72 lines (51 loc) · 2.32 KB
/
cachematrix.R
File metadata and controls
72 lines (51 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
## NOTE: I would like to thank Daniele Pigni for the helpful explanation of the
## examples for this assignment. The explanation can be found at
## https://class.coursera.org/rprog-013/forum/thread?thread_id=694. The many
## other posts on the forums were also helpful to me in completing this
## assignment, including inspiration for using "####..." dividers.
##############################################################
## Function 1: makeCacheMatrix
## The function makeCacheMatrix defines four functions: set, get, setmatrix, and
## getmatrix. These sub-functions allow the user to set a matrix or its inverse,
## and to retrieve the previously-cached matrix or previously-cached inverse.
## TIP: In order to access the sub-functions, one needs to use use the '$' symbol as used in subsetting:
## makeCacheMatrix(x)$get(), etc. The command is less lengthy if one assigns
## makeCacheMatrix(x) to a new variable z in the console, so that one can use
## the command z$get(), etc.
makeCacheMatrix <- function(x = matrix()) {
# Part 1: Creates an empty variable i.
i <- NULL
# Part 2: The set, get, setinverse, and getinverse functions
set <- function(y) {
x <<- y
i <<- NULL
}
get <- function() x
setinverse <- function(inverse) i <<- inverse
getinverse <- function() i
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
###################################################################
## Function 2: cacheSolve
## The function cacheSolve first checks to see if the inverse of a matrix x has
## already been calculated and cached. If it has, the function retrieves the
## inverse matrix; if not, the function calculates the inverse of the
## matrix, caches it for later use, and displays it. The "m" argument of this
## function should correspond to makeCacheMatrix(x).
cacheSolve <- function(m, ...) {
# Part 1: Checks if inverse of the matrix has already been cached; if so, it
# returns the cached inverse.
i <- m$getinverse()
if(!is.null(i)) {
message("getting cached data")
return(i)
}
data <- m$get()
# Part 2: Solves for the inverse of the matrix. This part assumes that the
# matrix is invertible, as per the assignment instructions.
i <- solve(data, ...)
m$setinverse(i)
i
}