Giter Site home page Giter Site logo

programmingassignment2's Introduction

Introduction

Welcome in the repository of my second programming assignment.

  1. makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse.
  2. cacheSolve: 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.

Function: makeCacheMatrix

The makeCacheMatrix function creates a special "matrix", which is able to cache a value

makeCacheMatrix <- function(x) {
        m <- NULL
        # Set the value of the Matrix
        set <- function(y) {
                x <<- y
                m <<- NULL
        }
        # Get the value of the Matrix
        get <- function() x
        # Set the value of the mean
        setInv <- function(inv) m <<- inv
        # Get the value of the mean
        getInv <- function() m
        #
        list(set = set, get = get,
             setInv = setInv,
             getInv = getInv)
}

The following function calculates the inverse of the special "matrix" created with the above function.

Function: cacheSolve

cacheSolve <- function(x, ...) {
        # Checks to see if the inverse has already been calculated. 
        m <- x$getInv()
        if(!is.null(m)) { 
                message("getting cached data")
                # Gets the inversed matrix from the cache and skips the computation.
                return(m)
        }
        data <- x$get()
        # Calculates the inverse of the data
        m <- solve(data, ...)
        # Sets the value of the mean in the cache via the setmean function.
        x$setInv(m)
        m
}

Run them:

Here are few lines to test the functions in R:

mat <- matrix(1:4, ncol = 2, nrow = 2, byrow = TRUE)
matC <- makeCacheMatrix(mat)
cacheSolve(matC)
cacheSolve(matC)

programmingassignment2's People

Contributors

rdpeng avatar readycyr avatar gustavdelius avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.