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
64 lines (47 loc) · 1.9 KB
/
Copy pathcachematrix.R
File metadata and controls
64 lines (47 loc) · 1.9 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
## Put comments here that give an overall description of what your
## functions do
# makeCacheMatrix() needs to create a list of elements for a "cached" matrix.
# There are four elements:
# A set function to set the value of the matrix and to use the <<- operator
# to put it in the "special" environment.
# A get function to return the value of the matrix
# A function to set the inverse of the matrix in the special env.
# A function to get the value of the inverse from the cached (set) value if it exists
# The function will be just like the makeVector sample except that
# the argument will be an R matrix and the calculation will use solve() instead
# of mean().
## Strategy:
# Practice to understand how to create and manage a matrix.
# This is just x <- matrix(..., nrow = , ncol =) (Make it square).
# Figure out how to use solve().
# This is just solve(matrix)
## Write a short comment describing this function
## makeCacheMatrix() creates a CacheMatrix object that can cache the calculation
## of its inverse.
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL # value of inverse starts as NULL
set <- function (y) {
x <<- y
inv <<- NULL # delete the saved inverse if the matrix is re-set
}
get <- function() x #return the matrix data
setinv <- function(solve) inv <<- solve(x) # Pass in solve to Calculate the inverse
getinv <- function() inv
# Build the CacheMatrix object
list(set = set, get = get, setinv = setinv, getinv = getinv)
}
## Write a short comment describing this function
# cacheSolve() takes a CacheMatrix object and returns its inverse.
# It calculates the inverse if needed, or returns it from cache
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getinv()
if (!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data, ...)
x$setinv(inv)
inv
}