forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f657dd
commit c9a6e79
Showing
1 changed file
with
30 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,36 @@ | ||
## Put comments here that give an overall description of what your | ||
## functions do | ||
|
||
## Write a short comment describing this function | ||
## Creates a special "matrix" object that can cache its inverse. | ||
|
||
makeCacheMatrix <- function(x = matrix()) { | ||
|
||
inverse <- NULL | ||
## modify the reference of matrix | ||
set <- function(newReference){ | ||
x <<- newReference | ||
inverse <<- NULL | ||
} | ||
## get the matrix | ||
get <- function() x | ||
## cache the inverse | ||
setinverse <- function(inverse) inverse <<- inverse | ||
## get the cached inverse | ||
getinverse <- function() inverse | ||
list(set= set, get = get, | ||
setinverse = setinverse, | ||
getinverse = getinverse) | ||
} | ||
|
||
|
||
## Write a short comment describing this function | ||
|
||
cacheSolve <- function(x, ...) { | ||
## Return a matrix that is the inverse of 'x' | ||
## Computes the inverse. If the inverse has been cached returns the cached value | ||
cacheSolve<- function(x, ...) { | ||
i <- x$getinverse() | ||
## If matrix is cached already return cached data | ||
if (!is.null(i)){ | ||
message("getting cached data") | ||
return(i) | ||
} | ||
## calculate the inverse | ||
data <- x$get() | ||
i <- solve(data, ...) | ||
## cache inverse | ||
x$setinverse(i) | ||
i | ||
} |