forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
39 lines (34 loc) · 1.22 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
## The following code calculates the inverse of a given matrix as input argument ...
## to makeCacheMatrix() function. Then cachesolve() can be used to either calculate ...
## the inteverse of the matrix or return the cashed value of the inverse from memory
## makeCacheMatrix() can get a metrix as an input argument and assign ...
## memory location to the inverse and the matrix itself.
## memory location can be display by just caling the faunction without assignment.
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setmat <- function(solve) m <<- solve
getmat <- function() m
list(set = set, get = get,
setmat = setmat,
getmat = getmat)
}
## cachesolve() get the output of makeCacheMatrix() which are memory locations ...
## with or without the values of inverse. If there is no inverse ...
## calculated (i.e. the inverse is null and memory location is empty) it will ...
## be calculated otherwise the cashed value from memory will be returen.
cachesolve <- function(x, ...) {
m <- x$getmat()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data, ...)
x$setmat(m)
m
}