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
49 lines (41 loc) · 1.4 KB
/
cachematrix.R
File metadata and controls
49 lines (41 loc) · 1.4 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
## (First all, forgive the grammaticals mistakes,
## i'm not a native english speaker)
## The next two functions give the funcionallity for
## solving de inverse of a matrix without doing it
## more than once: If it was calculated before, then
## you'll get the value cached
## 'makeCacheMatrix' is a function that returns an object
## that can hold the values of a matrix and its inverse
## by providing functions to: get/set the matrix cached,
## and get/set the inverse of that matrix
makeCacheMatrix <- function(x = matrix()) {
invMatrix <<- NULL
setMat <- function(y) {
x <<- y
invMatrix <<- NULL
}
getMat <- function() x
setInv <- function(inverse) invMatrix <<- inverse
getInv <- function() invMatrix
list(setMat = setMat,
getMat = getMat,
setInv = setInv,
getInv = getInv)
}
## 'cacheSolve' is a function that, given an object 'x' (wich type
## is that returned by 'makeCacheMatrix'), it returns the inverse
## of the matrix first checking if that inverse function is already
## cached, if not, then solves the inverse of the matrix inside 'x',
## it saves it in the cache, and returns it
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inverse <- x$getInv()
if(!is.null(inverse))
{
return(inverse)
}
m <- x$getMat()
inverse <- solve(m)
x$setInv(inverse)
inverse
}