Repository navigation
Expand file tree
/
Copy pathcachematrix.R
More file actions
58 lines (44 loc) · 1.56 KB
/
Copy pathcachematrix.R
File metadata and controls
58 lines (44 loc) · 1.56 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
##This function creates a matrix object that can cache its inverse.
makeCacheMatrix <- function(x = matrix()) {
# Initially set to NULL
# Changes when the user sets the value
inv <- NULL
# set function
# Sets the matrix itself but not the inverse
set <- function(y) {
x <<- y
inv <<- NULL
}
# get function
# Gets the matrix itself but not the inverse
get <- function() x
# Manually set the inverse
setinverse <- function(inverse) inv <<- inverse
# Get the inverse
getinverse <- function() inv
# Encapsulate into a list
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Write a short comment describing this function
cacheSolve <- function(x, ...) {
# Get the current state of the inverse and see if it
# has been computed yet
inv <- x$getinverse()
# If it has...
if(!is.null(inv)) {
# Simply return the computed inverse
message("Getting cached matrix")
return(inv)
}
# If it hasn't...
# Get the matrix itself
data <- x$get()
# Find the inverse
inv <- solve(data, ...)
# Cache this result in the object
x$setinverse(inv)
# Return this new result
inv
}