forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
35 lines (30 loc) · 925 Bytes
/
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
## Matrix inversion is usually a costly computation. It might be better to cache
## inverse of matrix instead of computing it repeatedly. Below functions will do
## the job.
## Cache matrix and its inversion.
## param x: original matrix.
makeCacheMatrix <- function(x = matrix()) {
solved <- NULL
set <- function(y) {
x <<- y
solved <<- NULL
}
get <- function() x
setSolved <- function(inverse) solved <<- inverse
getSolved <- function() solved
list(set = set, get = get, setSolved = setSolved, getSolved = getSolved)
}
## Call makeCacheMatrix to get the inverse of a matrix
## param x: result of calling function makeCacheMatrix.
cacheSolve <- function(x, ...) {
solved = x$getSolved()
if(!is.null(solved)) {
message("Get cached inverse.")
return(solved)
}
data = x$get()
solved = solve(data, ...)
x$setSolved(solved)
## Return a matrix that is the inverse of 'x'
solved
}