forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
40 lines (35 loc) · 1.1 KB
/
cachematrix.R
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 below two functions are used to create a special object that stores a matrix and cache's its inverse
## Create a matrix of functions that:
## 1) sets the value of the matrix
## 2) gets the value of the matrix
## 3) sets the value of the inverse of the matrix
## 4) gets the value of the inverse of the matrix
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setinverse <- function(inverse) m <<- inverse
getinverse <- function() m
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## If the inverse of 'x' has already been cached, returned the cached data
## otherwise calculate the inverse of x and return x
cacheSolve <- function(x, ...) {
m <- x$getinverse()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data, ...)
x$setinverse(m)
m
}
x = matrix(c(7,0,-3,2, 3, 4, 1, -1, -2),3,3)
test <- makeCacheMatrix(x)
cacheSolve(test)