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
32 lines (27 loc) · 950 Bytes
/
cachematrix.R
File metadata and controls
32 lines (27 loc) · 950 Bytes
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
## The functions here are used to cache the inverse of a matrix
## Create an instance of a matrix which also stores its inverse
makeCacheMatrix <- function(x = matrix()) {
inverse_matrix <- NULL
set <- function(m){
x <<- m
inverse_matrix <- NULL
}
get <- function() x
setinverse <- function(inv_mat) inverse_matrix <- inv_mat
getinverse <- function() inverse_matrix
list(set=set, get=get, setinverse = setinverse, getinverse=getinverse)
}
## Function to get the inverse of a matrix from cache (if it exists in cache)
## create the inverse and store in cache
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inverse_matrix <- x$getinverse()
if(!is.null(inverse_matrix)){
message("getting cached data")
return(inverse_matrix)
}
data <- x$get
inverse_matrix <- solve(x)
x$setinverse(inverse_matrix)
inverse_matrix
}