Skip to content

Assignment 2 #3380

@rustik-9704

Description

@rustik-9704

makeCacheMatrix <- function(x = matrix()) {

  • inv <- NULL
    
  • set <- function(y) {
    
  •     x <<- y
    
  •     inv <<- NULL
    
  • }
    
  • get <- function() x
    
  • setInverse <- function() inv <<- solve(x) #calculate the inverse
    
  • getInverse <- function() inv
    
  • list(set = set,
    
  •      get = get,
    
  •      setInverse = setInverse,
    
  •      getInverse = getInverse)
    
  • }

cacheSolve <- function(x, ...) {

  • ## Return a matrix that is the inverse of 'x'
    
  • inv <- x$getInverse()
    
  • if (!is.null(inv)) {
    
  •     message("getting cached data")
    
  •     return(inv)
    
  • }
    
  • mat <- x$get()
    
  • inv <- solve(mat, ...)
    
  • x$setInverse(inv)
    
  • inv
    
  • }

funs <- makeCacheMatrix()
funs$set(matrix(1:4, 2))
funs$get()
[,1] [,2]
[1,] 1 3
[2,] 2 4
funs$setInverse()
funs$getInverse()
[,1] [,2]
[1,] -2 1.5
[2,] 1 -0.5

Activity

jingxuanxiao

jingxuanxiao commented on Aug 20, 2018

@jingxuanxiao

I really like the fact that test samples are giving in the end.
Also the variable name as 'inv' is very self explanatory.
Well done.

Hemant0509

Hemant0509 commented on Sep 18, 2018

@Hemant0509

The logic is quite clear, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jingxuanxiao@rustik-9704@Hemant0509

        Issue actions

          Assignment 2 · Issue #3380 · rdpeng/ProgrammingAssignment2