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
50 lines (41 loc) · 1.73 KB
/
cachematrix.R
File metadata and controls
50 lines (41 loc) · 1.73 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
45
46
47
48
49
50
## These functions allows to reuse the inverse of a matrix without
## computed each time is need it. You must use 'makeCacheMatrix'
## to generate a cache version of your matrix the first time. After,
## you can compute or get the inverse using 'cacheSolve'. If you need
## the inverse of a new matrix, you can use the set function of
## cached matrix in order to update the matrix value.
## Generate a list of functions that can be used to caching a matrix and its
## inverse, in order to avoid the inverse computation of the same matrix
## multiple times.
makeCacheMatrix <- function(x = matrix()) {
## 'x' is the matrix which inverse must be cached.
## Return a list of functions that can be used to caching a matrix
## and its inverse.
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setinverse <- function(invers) inv <<- invers
getinverse <- function() inv
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Compute the inverse of a matrix if it has not been computed before.
## Or just return the inverse value in cache if this was already computed.
cacheSolve <- function(x, ...) {
## 'x' is a vector generaed with 'makeCacheMatrix' and represents
## the a cached version of the matrix whcih inverse we want to compute.
## Return a matrix that is the inverse of 'x'
inv <- x$getinverse()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data, ...)
x$setinverse(inv)
inv
}