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
44 lines (27 loc) · 1.1 KB
/
Copy pathcachematrix.R
File metadata and controls
44 lines (27 loc) · 1.1 KB
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
## These functions were made for preventing excessive calculation in inversing matrix
## It cache inversed matrix and won't inverse it again, just return cached value
## It make list with get/set functions with captured state which has two matrix: orignal and inversed
## inversed set to NULL, it will get value when happened first access to inversed matrix with function cacheSolve
makeCacheMatrix <- function(x = matrix()) {
set <- function(new_m) {
m <<- new_m
solved <<- NULL
}
set(x)
get <- function() m
setinverse <- function(inverse) solved <<- inverse
getinverse <- function() solved
list(set = set, get = get, setinverse = setinverse, getinverse = getinverse)
}
## Return a matrix that is the inverse of 'x' and cache the inversed value
## Next request will return cached value without any calculations
cacheSolve <- function(x, ...) {
ret <- x$getinverse()
if(!is.null(ret)) {
message("getting cached data")
return(ret)
}
ret <- solve(x$get(), ...)
x$setinverse(ret)
ret
}