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
57 lines (51 loc) · 1.98 KB
/
cachematrix.R
File metadata and controls
57 lines (51 loc) · 1.98 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
## Matrix inversion is usually a costly computation and there
## may be some benefit to caching the inverse of a matrix rather
## than compute it repeatedly. The functions makeCacheMatrix and
## cacheSolve implement this caching for matrix inversion.
# R Programming
# Programming Assignment 2
# by Thiago Akio Nakamura
# akionakas@gmail.com
## 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.
# To create the matrix, it receives the same arguments as the
# matrix() function, thus allowing the direct usage of the cached
# version, without needing the previously call the matrix() fuction.
makeCacheMatrix <- function(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL){
# Create matrix
x <- matrix(data = data, nrow = nrow, ncol = ncol, byrow = byrow, dimnames = dimnames)
# Initialize inverse value with NULL
inv <- NULL
# Modify matrix
set <- function(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL){
# Create modified matrix
x <<- matrix(data = data, nrow = nrow, ncol = ncol, byrow = byrow, dimnames = dimnames)
# Set the inverse back to NULL since the matrix has changed
inv <<- NULL
}
# Return the matrix itself
get <- function() x
# Set the inverse value of the matrix
setInv <- function(i) inv <<- i
# Return the cached inverse value
getInv <- function() inv
# Return the list object
list(set = set, get = get, setInv = setInv, getInv = getInv)
}
## This function creates a special "matrix" object that can cache its inverse.
cacheSolve <- function(x, ...){
# Get the cached inverse value
inv <- x$getInv()
# If there is a value, return that
if(!is.null(inv)){
message("Getting cached data.")
return(inv)
}
# If there is no cache, get the matrix and compute the inverse
data <- x$get()
inv <- solve(data)
x$setInv(inv) # Set the inverse value
inv
}