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
54 lines (40 loc) · 1.32 KB
/
cachematrix.R
File metadata and controls
54 lines (40 loc) · 1.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
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
makeCacheMatrix <- function(x = matrix()) {
## initialize local variable
inv <- NULL
##1 of 4 function definition. Sets the cached matrix value
set <- function(y) {
x <<- y ## assign value to a variable in the parent environment.
inv <<- NULL
}
##2 of 4 function definition; returns the matrix
get <- function() x
##3 of 4 function definition. sets the cached inverse
setinv <- function(inverse) inv <<- inverse
##4 of 4 function definition, returns the cached inverse
getinv <- function() inv
list(set = set, get = get,
setinv = setinv,
getinv = getinv)
}
## Write a short comment describing this function
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
## sets the local variable to the inverse
inv <- x$getinv()
## if inv is not null, it will get cached data
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
## if inv is null, the function will obtain the matrix from the makeCacheMatrix object and assign it to data
data <- x$get()
## get the inverse
inv <- solve(data, ...)
## set the inverse of matrix
x$setinv(inv)
## returns the inverse
inv
}