Skip to content

Commit d3b93c2

Browse files
committed
Update of practice - 26-04-2014, 17:44
1 parent 38b244b commit d3b93c2

File tree

1 file changed

+49
-23
lines changed

1 file changed

+49
-23
lines changed

cachematrix.R

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
11
## Put comments here that give an overall description of what your
22
## functions do
33

4+
##### Calculate de inverse matrix, storing this matrix to avoid its calculation again
5+
6+
7+
48
## Write a short comment describing this function
59

10+
##### makeCacheMatrix
11+
#####
12+
##### Store the data of a created matrix. The store is composed to a list with 4 elements:
13+
##### 1 - assingment of the matrix (set)
14+
##### 2 - show the matrix (get)
15+
##### 3 - calculate de inverse matrix (setinverse)
16+
##### 4 - show the inverse matrix (getinverse)
17+
618
makeCacheMatrix <- function(x = matrix()) {
7-
cachedmatrix <- NULL
8-
set <- function(y) {
19+
cachedmatrix <- NULL # create an empty object, which is used to store the returned values
20+
21+
set <- function(y) { # create a function to assign the value of the matrix
922
x <<- y
10-
cachedmatrix <<- NULL
11-
}
12-
get <- function() {
23+
cachedmatrix <<- NULL
24+
}
25+
26+
get <- function() { # create a function to show the matrix
1327
x
14-
}
15-
setinverse <- function(solve) {
16-
cachedmatrix <<- solve
17-
}
18-
getinverse <- function() {
19-
cachedmatrix
20-
}
28+
}
29+
30+
setinverse <- function(solve) { # create a function to calculate the inverse matrix
31+
cachedmatrix <<- solve
32+
}
33+
34+
getinverse <- function() { # create a function to show the inverse matrix
35+
cachedmatrix
36+
}
37+
38+
# create a list, which elements are been described above
2139
list(set = set, get = get,
2240
setinverse = setinverse,
2341
getinverse = getinverse)
@@ -26,16 +44,24 @@ makeCacheMatrix <- function(x = matrix()) {
2644

2745
## Write a short comment describing this function
2846

47+
##### Calculate the invese matrix. In the case, it is the first time, it calculates the
48+
##### inverse matrix, else it returns the inverse matrix calculated previously, which is
49+
##### obtained calling getinverse() function.
50+
2951
cacheSolve <- function(x, ...) {
30-
## Return a matrix that is the inverse of 'x'
31-
32-
cachedmatrix <- x$getinverse()
33-
if(!is.null(cachedmatrix)) {
34-
message("getting cached data")
35-
return(cachedmatrix)
36-
}
37-
data <- x$get()
38-
cachedmatrix <- solve(data)
39-
x$setinverse(cachedmatrix)
40-
cachedmatrix
52+
## Return a matrix that is the inverse of 'x'
53+
54+
cachedmatrix <- x$getinverse() # obtain the inverse matrix
55+
56+
# if it was calculated previously (x$getinverse() returns the inverse matrix)
57+
if(!is.null(cachedmatrix)) {
58+
message("getting cached data")
59+
return(cachedmatrix) # return the inverse matrix
60+
}
61+
62+
# else
63+
data <- x$get() # if the inverse matrix was not calculated previously
64+
cachedmatrix <- solve(data) # calculate the inverse matrix
65+
x$setinverse(cachedmatrix) # store the inverse matrix in the list
66+
cachedmatrix # return the inverse matrix
4167
}

0 commit comments

Comments
 (0)