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
99 lines (71 loc) · 2.19 KB
/
cachematrix.R
File metadata and controls
99 lines (71 loc) · 2.19 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
## Programming Assignment 2: Lexical Scoping
## Edwin Torres
# For testing the functions please use the code
# commented and the end of the file
# makeCacheMatrix: This function creates a special "matrix" object
# that can cache its inverse.
# x matrix
# I inverse matrix
makeCacheMatrix <- function(x = matrix()) {
I <- NULL
## Set or reset the matrix x
set <- function(y) {
x <<- y
#Reset the inverse matrix I
I <<- NULL
}
## Get the matrix x
get <- function() x
## Set the inverse matrix x
setInverse <- function(inverse) I <<- inverse
## Get the inverse matrix
getInverse <- function() I
list(set = set
,get = get
,setInverse = setInverse
,getInverse = getInverse)
}
# cacheSolve: This function computes the inverse of the special
# "matrix" returned by makeCacheMatrix above. 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, ...) {
## Return a matrix that is the inverse of 'x'
I <- x$getInverse()
## If exists return a cached inverse matrix
if(!is.null(I)) {
message("Getting cached data.")
return(I)
}
## Get the matrix from makeCacheMatrix object
data <- x$get()
## Inverse the matrix
I <- solve(data) %*% data
##Set the inverse in the makeCacheMatrix object
x$setInverse(I)
##Return the inverse matrix
I
}
##########################################
### Code for testing the functions
### Uncomment the lines of code
### to test the functions
## The following matrix M was obteined from:
## http://www.purplemath.com/modules/mtrxinvr.htm
## You can chech the inverse matrix on the URL
#M <- matrix(c(1,1,1,3,4,2,3,3,4),3,3)
## Create the object makeCacheMatrix named y
#y <- makeCacheMatrix()
## Set the matrix M to object y
#y$set(M)
## Get the matrix M from y to test the function
#y$get()
## Test getInverse, in this step must be null
#y$getInverse()
## Calculate inverse matrix of y for the first time
#cacheSolve(y)
## Test function getInverse
#y$getInverse()
## Calculate inverse matrix of y for the second time
## this time the cached data
#cacheSolve(y)