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
53 lines (40 loc) · 1.53 KB
/
cachematrix.R
File metadata and controls
53 lines (40 loc) · 1.53 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
51
52
53
## The functions allow caching the values of a matrix and its inverse in a special object
## These stored values can be recovered instead of being computerized again.
## FUNCTION 1: makeCacheMatrix()
## It generates a cacheable object that includes a list with four functions
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
# 1st Function - set the values of the matrix in 'x'
set <- function(y) {
x <<- y
m <<- NULL
}
# 2nd Function - get the values of the stored matrix of 'x'
get <- function() x
# 3rd Function - set the values of the inverse matrix in 'x'
setsolve <- function(solve) m <<- solve
# 4th Function - get the values of the stored inverse matrix of 'x'
getsolve <- function() m
list(set = set, get = get,
setsolve = setsolve,
getsolve = getsolve)
}
## FUNCTION 2: cacheSolve()
## Function that calculates the inverse of the special "matrix" created
## and cached with the function 'makeCacheMatrix'
cacheSolve <- function(x, ...) {
# if the inverse matrix is cached the function retrieves their values
# and displays a message
m <- x$getsolve()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
# If the inverse matrix is not cached the function calculates the
# inverse of the matrix and caches
data <- x$get()
m <- solve(data, ...)
x$setsolve(m)
# shows the inverse matrix on screen
m
}