forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
35 lines (29 loc) · 1.19 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
## Put comments here that give an overall description of what your
## functions do
## This function computes the makeCacheMatrix of the special matrix. Returns a list object containing the given matrix 'x' and set the cache for 'm' and 'x'
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setcachematrix <- function(cachematrixinverse) m <<- cachemactrixinverse
getcachematrix <- function() m
list(set = set, get = get,
setcachematrix = setcachematrix,
getcachematrix = getcachematrix)
}
## This function computes the inverse of the special "matrix" if is not inverse.
## If the inverse has already been calculated (and the matrix has not changed), then the cachesolve should retrieve the inverse from the cache.
cacheSolve <- function(x, ...) {
m <- x$getcachematrix()
if(solve(m) != m) {
data <- x$get()
m <- solve(data, ...)
x$setcachematrix(m)
return(m)
} else
message("getting inverse cached matrix")
m
}