Open williamchoo opened 4 years ago
makeCacheMatrix <- function(x = matrix()) { i <- NULL set <- function(y) { x <<- y i <<- NULL } get <- function() x setinverse <- function(inverse) inv <<- inverse getinverse <- function() i list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) }
cacheSolve <- function(x, ...) { i <- x$getinverse() if (!is.null(i)) { message("getting cached data") return(i) } data <- x$get() i <- solve(data, ...) x$setinverse(i) i }
https://github.com/williamchoo/ra2/blob/master/cachematrix.R
https://github.com/williamchoo/ra2/commit/9605d59df45f9d7da503b4a5d112d4310dda56d3
I reaches the same result
makeCacheMatrix: This function creates a special “matrix” object that can cache its inverse.
Computing the inverse of a square matrix can be done with the solve function in R.
For example, if X is a square invertible matrix, then solve(X) returns its inverse
The purpose of these functions are to cache the inverse of a matrix rather than computing it repeatedly.
The first function, makeCacheMatrix, creates a special "matrix" object that can cache its inverse, which is list to do the following:
set the value of the vector
get the value of the vector
set the value of the mean
get the value of the mean
makeCacheMatrix <- function(x = matrix()) { i <- NULL set <- function(y) { x <<- y i <<- NULL } get <- function() x setinverse <- function(inverse) inv <<- inverse getinverse <- function() i list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) }
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 cacheSolve should retrieve the inverse from the cache.
cacheSolve <- function(x, ...) { i <- x$getinverse() if (!is.null(i)) { message("getting cached data") return(i) } data <- x$get() i <- solve(data, ...) x$setinverse(i) i }
Return a matrix that is the inverse of 'x'
https://github.com/williamchoo/ra2/blob/master/cachematrix.R
https://github.com/williamchoo/ra2/commit/9605d59df45f9d7da503b4a5d112d4310dda56d3