forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
75 lines (61 loc) · 1.75 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/R
#
# this script contain 2 functions to create a cache for inverted Matrix
#
# to use this functions, need to create a cacheMatrix, at first,
# with the cacheSolve, cann you create an new inverted matrix or use
# cache of inverted matrix, if it exists.
#
# Small example:
## source("cachematrix.R")
## cachedMatrix = makeCacheMatrix( matrix(rnorm(4), 2, 2) )
## cacheSolve(cachedMatrix)
## This function make cached inverted Matrix
# input: Matrix
# output: cached inverted Matrix
makeCacheMatrix <- function(x = matrix()) {
## set cache to null
cached_matrix <- NULL
## set matrix and cache
set <- function(y) {
x <<- y
cached_matrix <<- NULL
}
## get matrix
get <- function() {
x
}
## set inverse to cached matrix
setinverse <- function(inverse) {
cached_matrix <<- inverse
}
## get inverse cached matrix
getinverse <- function() {
cached_matrix
}
### return matrix
list(set = set,
get = get,
setinverse = setinverse,
getinverse = getinverse)
}
### This function return cached inverted Matrix
# input: cached matrix
# output: cached inverted matrix or new created inverted matrix if cache is null
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
## get cache
cached_matrix <- x$getinverse()
## if cache exists return this
if(!is.null(cached_matrix)) {
message("getting cached inverted matrix")
return(cached_matrix)
}
## make cache of invert matrix
data <- x$get()
inverse <- solve(data, ...)
## set cache for inverted matrix
x$setinverse(inverse)
## return inverted matrix
return(inverse)
}