forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
36 lines (29 loc) · 1.02 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
## makeCacheMatrix creates a special object, a list. It contains a function to:
## 1. Set the value of matrix. 2. Get the value of matrix.
## 3.Set the value of inverse matrix. 4.Get the value of inverse matrix.
## Pass matrix into makeCacheMatrix to create a special object
makeCacheMatrix <- function(x = matrix()) {
inversed <- NULL
set <- function(y) {
x <<- y
inversed <<- NULL
}
get <- function() x
setinverse <- function(inverse) inversed <<- inverse
getinverse <- function() inversed
list(set=set, get=get, setinverse=setinverse, getinverse=getinverse)
}
## Pass the special object to CacheSolve in order to get it's inverse form.
## If it was called before and special object is the same, it'll get the cached data.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inversed <- x$getinverse()
if(!is.null(inversed)) {
message("getting cached data.")
return(inversed)
}
data <- x$get()
inversed <- solve(data)
x$setinverse(inversed)
inversed
}