forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
47 lines (41 loc) · 1.35 KB
/
cachematrix.R
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
## The following functions are designed to create a matrix object which
## can store its value and its inverse. The values of the matrix can
## also be modified.
## This function creates the matrix object. The following actions can be
## performed by calling the right function considering the object as a list:
## x$set(), set the matrix.
## x$get(), gets the value of the matrix.
## x$setR(), set the reverse of the matrix.
## x$getR(), get the reverse of the matrix.
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setR <- function(solve) m <<- solve
getR <- function() m
list(set = set, get = get,
setR = setR,
getR = getR)
}
## This function computes the inverse of the matrix x.
## If the inverse has already being computed, the matrix returns the cached copy.
## Otherwise, it computes the inverse.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
m <- x$getR()
if(!is.null(m)) {
## A cached inverse has been found. Returning the cached data:
message("getting cached data")
return(m)
}
## Getting the matrix
data <- x$get()
# Computing the inverse:
m <- solve(data, ...)
## Assigning the inverse to the matrix object argument
x$setR(m)
m
}