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
39 lines (36 loc) · 2.37 KB
/
cachematrix.R
File metadata and controls
39 lines (36 loc) · 2.37 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
## 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.
## This function creates a special "matrix" object that can cache its inverse.
## The first function, makeCacheMatrix creates a special "matrix", which is
## really a list containing a function to
## 1. set the value of the vector
## 2. get the value of the vector
## 3. set the value of the mean
## 4. get the value of the mean
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL ## inv is assigned the value NULL
set <- function(y) { ## set function
x <<- y ## substitutes the matrix x with matrix y
inv <<- NULL ## inv is assigned the value NULL
}
get <- function() x ## get function returns the original matrix
setinverse <- function(inverse) inv <<- inverse ## inv is assigned the value inverse
getinverse <- function() inv ## computes the inverse of the matrix
list(set = set, get = get, setinverse = setinverse, getinverse = getinverse)
} ## stores the value of set, get, setinverse, and getinverse in a list
## The function below computes the inverse of the special "matrix" returned by
## makeCacheMatrix above. it first checks to see if the inverse has already been
## calculated. If so, it gets the inverse from the cache and skips the computation.
## Otherwise, it calculates the inverse of the matrix and sets the value of the
## inverse in the cache via the setinv function.
cacheSolve <- function(x, ...) {
inv <- x$getinverse() ## getinverse obtains the stored computed inverse of the matrix
if(!is.null(inv)) { ## if there is a stored value for the inverse of the matrix
message("getting cached data") ## the message "getting cached data" is returned and
return(inv) ## the value of the stored computed inverse of the matrix is returned
} ## if there is no stored value for the inverse of the matrix
data <- x$get() ## the original matrix is retrieved and
inv <- solve(data, ...) ## the inverse of the matrix is computed and
x$setinverse(inv) ## the inverse of the matrix is stored
inv
}