-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
executable file
·46 lines (37 loc) · 1.53 KB
/
Copy pathcachematrix.R
File metadata and controls
executable file
·46 lines (37 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
## This code calculates the inverse of a matrix and caches its value.
## When the function that calculates the matrix inverse is called
## again, it checks it the matrix has been updated. If the matrix is
## unchanged, it returns the cached value; if not, it recalculates the
## inverse
## makeCacheMatrix() makes a list that accepts a matrix 'x' and
## stores 'x' and its inverse in a list
makeCacheMatrix <- function(x = matrix()) {
m <- NULL ## This variable will store the inverse. Initialize it as NULL
set <- function(y) {
x <<- y ## set x as the matrix
m <<- NULL ## set inverse as NULL
}
get <- function() x ## get the matrix
## store the value of inverse in the variable 'm'
setinverse <- function(inverse) m <<- inverse
## get the value of the inverse
getinverse <- function() m
## return the information of the matrix and its inverse as a list
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## This function returns the inverse of the matrix 'x'
cacheSolve <- function(x, ...) {
m <- x$getinverse() ## get inverse
## If 'm' is not NULL, it means that the matrix 'x'
## is unchanged, so get cached value of the inverse
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get() ## get the matrix x and store it in variable 'data'
m <- solve(data, ...) ## calculate inverse of x
x$setinverse(m) ## set the inverse of x
m
}