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
40 lines (35 loc) · 1.37 KB
/
cachematrix.R
File metadata and controls
40 lines (35 loc) · 1.37 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
## The following two functions find the inverse of a matrix.
## If the matrix has already been inverted by cacheSolve, then
## the solution is retrieved from cached values.
## makeCacheMatrix sets the value of the matrix, gets the value of the matrix,
## sets the inverse of the matrix and gets the inverse of the matrix. The
## function then makes a list of 4 functions: set, get, getinverse, and setinverse.
makeCacheMatrix <- function(x = matrix()) {
invert <- NULL
set <- function(y) {
x <<- y
invert <<- NULL
}
get <- function() x
setinverse <- function(inverse) invert <<- inverse
getinverse <- function() invert
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## cacheSolve returns the inverse of a matrix. If the matrix has already
## been inverted by these functions, it returns the cached solution. If
## the input is a unique matrix cacheSolve solves for the inverse of the
## matrix.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
invert <- x$getinverse()
if(!is.null(invert)) {
message("getting cached data")
return(invert)
}
data <- x$get()
invert <- solve(data, ...)
x$setinverse(invert)
invert
}