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
34 lines (31 loc) · 1.01 KB
/
Copy pathcachematrix.R
File metadata and controls
34 lines (31 loc) · 1.01 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
## Functions for special "matrices" to compute and cache the matrix inverse
## Creates a special "matrix" object that can cache its inverse
makeCacheMatrix <- function(x = matrix()) {
matrix_inverse <- NULL
set <- function(y) {
x <<- y
matrix_inverse <<- NULL
}
get <- function() x
setinverse <- function(solved) matrix_inverse <<- solved
getinverse <- function() matrix_inverse
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Computes the inverse of the special "matrix" returned by makeCacheMatrix
## If the inverse has already been calculated, retrieves it from cache
## The matrix supplied must be invertible
cacheSolve <- function(x) {
## Return a matrix that is the inverse of 'x'
matrix_inverse <- x$getinverse()
if(!is.null(matrix_inverse)) {
message("getting cached data")
return(matrix_inverse)
}
message("calculating matrix inverse")
data <- x$get()
matrix_inverse <- solve(data)
x$setinverse(matrix_inverse)
matrix_inverse
}