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
61 lines (47 loc) · 1.77 KB
/
cachematrix.R
File metadata and controls
61 lines (47 loc) · 1.77 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
# Written by Larry Mannings, 25 November 2025
# makeCacheMatrix() function leverages the makeVector example from R Programming
# Ass. 2, modified to create a list consisting of cached matrix and
# corresponding inverse.
makeCacheMatrix <- function(x = numeric()) {
# x is numeric matrix object representing an invertible matrix
# i is matrix object representing inverse of matrix object (x)
# Initialize the inverse matrix to NULL
i <- NULL
# Set the value of the matrix
set <- function(y) {
x <<- y
i <<- NULL
}
# Get the value of the matrix
get <- function() x
# Set the value of the inverse matrix
setinv <- function(inv) i <<- inv
# Get the value of the inverse matrix
getinv <- function() i
# Store objects in a list and return
list(set = set, get = get,
setinv = setinv,
getinv = getinv)
}
#---------------------------------------------------------------------------
# cacheSolve() function leverages the cachemean() example from R Programming
# Ass. 2, modified to compute the inverse of the returned by
# makeCacheMatrix() function.
cacheSolve <- function(x, ...) {
# x is list object representing constructed from the makeCacheMatrix
# function
# i is matrix object representing inverse of matrix object (x)
# Call the getinv(function) as a list object to get matrix inverse
i <- x$getinv()
# If the inverse has already been calculated retrieve from the cache
if(!is.null(i)) {
message("getting cached data")
return(i)
}
# Else, retrieve the cached matrix and solve for the inverse matrix
data <- x$get()
i <- solve(data, ...)
# Cache the inverse matrix and return value
x$setinv(i)
i
}