rdpeng / ProgrammingAssignment2

Repository for Programming Assignment 2 for R Programming on Coursera
838 stars 143.47k forks source link

Programming Assignment 2: Lexical Scoping #4778

Open williamchoo opened 4 years ago

williamchoo commented 4 years ago

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

maryamosama217 commented 4 years ago

I reaches the same result