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
38 lines (34 loc) · 941 Bytes
/
Copy pathcachematrix.R
File metadata and controls
38 lines (34 loc) · 941 Bytes
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
## The functions will compute the inverse of a square matrix, if it's already computed
##it will recover the result from the cache
#The function "makeCacheMatrix" creates a matrix object and can cache its inverse.
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setInv <- function(solve) m <<- solve
getInv <- function() m
list(set = set, get = get,
setInv = setInv,
getInv = getInv)
}
#The function "CacheSolve" computes the inverse of the matrix returned by
#makeCacheMatrix, and if its already calculate it recover from cache.
cacheSolve <- function(x, ...) {
m <- x$getInv()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data, ...)
x$setInv(m)
m
}
##Example of use:
example_matrix <- matrix(1:4,2,2)
a <- makeCacheMatrix(example_matrix)
cacheSolve(a)
cacheSolve(a)