Instantly share code, notes, and snippets.

## DouglasRubin / Week 3 programming assignment_b.r

- Star 1 You must be signed in to star a gist
- Fork 92 You must be signed in to fork a gist

## nithyabk commented May 7, 2020

Put comments here that give an overall description of what your, functions do, our aim in this experiment is to write a pair of functions, namely,, "makecachematrix" and "cachesolve" that cache the inverse of a matrix, write a short comment describing this function, makecachematrix is a function which creates a special "matrix" object that can, cache its inverse for the input (which is an invertible square matrix).

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

inv <- NULL set <- function(y) { x <<- y inv <<- NULL } get <- function() x setinv <- function(inverse) inv <<- inverse getinv <- function() inv list(set = set, get = get, setinv = setinv, getinv = getinv) }

## cacheSolve is a function which 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 the cachesolve should retrieve the, inverse from the cache.

cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' inv <- x$getinv() if(!is.null(inv)) { message("getting cached result") return(inv) } data <- x$get() inv <- solve(data, ...) x$setinv(inv) inv }

## ---------------Checking the program------------------------

M <- matrix(rnorm(16),4,4), m1 <- makecachematrix(m), cachesolve(m1), [,1] [,2] [,3] [,4], [1,] -0.1653269 0.2592203 0.6176218 -0.7520955, [2,] 0.2828334 -0.1853499 0.4511382 0.2094365, [3,] 0.1434840 1.0413868 -0.3550853 -0.3261154, [4,] 0.1793583 -0.4252171 -0.4371493 -0.1749830.

Sorry, something went wrong.

## nithyabk commented May 9, 2020

Caching the inverse of a matrix:, matrix inversion is usually a costly computation and there may be some, benefit to caching the inverse of a matrix rather than compute it repeatedly., below are a pair of functions that are used to create a special object that, stores a matrix and caches its inverse., this function creates a special "matrix" object that can cache its inverse..

makeCacheMatrix <- function(x = matrix()) { inv <- NULL set <- function(y) { x <<- y inv <<- NULL } get <- function() x setInverse <- function(inverse) inv <<- inverse getInverse <- function() inv list(set = set, get = get, setInverse = setInverse, getInverse = getInverse) }

## This function computes the inverse of the special "matrix" created by

Makecachematrix above. if the inverse has already been calculated (and the, matrix has not changed), then it should retrieve the inverse from the cache..

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 }

testing my function

source("ProgrammingAssignment2/cachematrix.R") my_matrix <- makeCacheMatrix(matrix(1:4, 2, 2)) my_matrix$get() [,1] [,2] [1,] 1 3 [2,] 2 4 my_matrix$getInverse() NULL cacheSolve(my_matrix) [,1] [,2] [1,] -2 1.5 [2,] 1 -0.5 cacheSolve(my_matrix) getting cached data [,1] [,2] [1,] -2 1.5 [2,] 1 -0.5 my_matrix$getInverse() [,1] [,2] [1,] -2 1.5 [2,] 1 -0.5 my_matrix$set(matrix(c(2, 2, 1, 4), 2, 2)) my_matrix$get() [,1] [,2] [1,] 2 1 [2,] 2 4 my_matrix$getInverse() NULL cacheSolve(my_matrix) [,1] [,2] [1,] 0.6666667 -0.1666667 [2,] -0.3333333 0.3333333 cacheSolve(my_matrix) getting cached data [,1] [,2] [1,] 0.6666667 -0.1666667 [2,] -0.3333333 0.3333333 my_matrix$getInverse() [,1] [,2] [1,] 0.6666667 -0.1666667 [2,] -0.3333333 0.3333333

## gaganrvn commented May 26, 2020

This is really well done! You do an excellent job coding, but one opinion is to stick to getInverse and setInverse so as to make your code easier to read. Your code is very well done, and a small bit of indenting would make it perfect. Well Done!

## nithyabk commented May 26, 2020

Thank you so much sir.

## Vemiya commented May 28, 2020

## NoeVelarde commented Aug 13, 2020

## okamahen commented Nov 12, 2020

Good documentation, but indentation will clearly make this better :)

## varelaz commented Feb 15, 2021

You didn't make a fork as was requested, but code is correct

## VerdySeptian commented May 10, 2021

## sherlu commented Aug 14, 2021

works well. Just the name of setsolve, getsolve etc are confusing because the work is to set/get inverse :)

## Seirakos commented Sep 16, 2021

good work :3

## Piku1997 commented Oct 31, 2021

## Dijusharon commented Feb 21, 2022

how to submit in github

## CaringPerson commented Jul 18, 2022

nice work and documentation

## abdullah30 commented Feb 14, 2023

I did not get the point that how we can submit the assignment. Can you help me in getting it? Regards Abdullah

## Saksham2805 commented Apr 23, 2023

## phume2010 commented Apr 23, 2023

Your code is easy to follow

## ZilolaEE commented Aug 22, 2023

## Famous Dojo

Work Harder.

## R Programming Week 3 Programming Assignment 2: Lexical Scoping

Introduction.

This second programming assignment will require you to write an R function that is able to cache potentially time-consuming computations. For example, taking the mean of a numeric vector is typically a fast operation. However, for a very long vector, it may take too long to compute the mean, especially if it has to be computed repeatedly (e.g. in a loop). If the contents of a vector are not changing, it may make sense to cache the value of the mean so that when we need it again, it can be looked up in the cache rather than recomputed. In this Programming Assignment you will take advantage of the scoping rules of the R language and how they can be manipulated to preserve state inside of an R object.

## Example: Caching the Mean of a Vector

In this example we introduce the <<- operator which can be used to assign a value to an object in an environment that is different from the current environment. Below are two functions that are used to create a special object that stores a numeric vector and caches its mean.

The first function, makeVector creates a special “vector”, which is really a list containing a function to

- set the value of the vector
- get the value of the vector
- set the value of the mean
- get the value of the mean

The following function calculates the mean of the special “vector” created with the above function. However, it first checks to see if the mean has already been calculated. If so, it get s the mean from the cache and skips the computation. Otherwise, it calculates the mean of the data and sets the value of the mean in the cache via the setmean function.

## Assignment: Caching the Inverse of a Matrix

Matrix inversion is usually a costly computation and there may be some benefit to caching the inverse of a matrix rather than computing it repeatedly (there are also alternatives to matrix inversion that we will not discuss here). Your assignment is to write a pair of functions that cache the inverse of a matrix.

Write the following functions:

- makeCacheMatrix : This function creates a special “matrix” object that can cache its inverse.
- 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.

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.

For this assignment, assume that the matrix supplied is always invertible.

Suggested Solution —

Tips for submitting assignment:

- Download GitHub Desktop
- Fork the GitHub repository containing the stub R files to create a copy under your own account.
- Clone your forked GitHub repository to your computer so that you can edit the files locally on your own machine. *Clone or Download, revise it in R or Rstudio*
- Commit( or Copy ) your completed R file into YOUR git repository and push your git branch to the GitHub repository under your account. *Or you can do pull request on Github.

Love to help more and more people as I keep going on the Specialization Track! Hope this helps!

## Share this:

Leave a reply cancel reply.

- Already have a WordPress.com account? Log in now.
- Follow Following
- Copy shortlink
- Report this content
- View post in Reader
- Manage subscriptions
- Collapse this bar

- Programming With R Assignment 2
- by Jeremias Lalis
- Last updated over 7 years ago
- Hide Comments (–) Share Hide Toolbars

Twitter Facebook Google+

## Or copy & paste this link into an email or IM:

## R Programming Assignment 2: Lexical Scoping

Introduction.

This second programming assignment will require you to write an R function is able to cache potentially time-consuming computations. For example, taking the mean of a numeric vector is typically a fast operation. However, for a very long vector, it may take too long to compute the mean, especially if it has to be computed repeatedly (e.g. in a loop). If the contents of a vector are not changing, it may make sense to cache the value of the mean so that when we need it again, it can be looked up in the cache rather than recomputed. In this Programming Assignment will take advantage of the scoping rules of the R language and how they can be manipulated to preserve state inside of an R object.

## Example: Caching the Mean of a Vector

In this example we introduce the <<- operator which can be used to assign a value to an object in an environment that is different from the current environment. Below are two functions that are used to create a special object that stores a numeric vector and caches its mean.

The first function, makeVector creates a special “vector”, which is really a list containing a function to

- set the value of the vector
- get the value of the vector
- set the value of the mean
- get the value of the mean

The following function calculates the mean of the special “vector” created with the above function. However, it first checks to see if the mean has already been calculated. If so, it gets the mean from the cache and skips the computation. Otherwise, it calculates the mean of the data and sets the value of the mean in the cache via the setmean function.

## Assignment: Caching the Inverse of a Matrix

Matrix inversion is usually a costly computation and there may be some benefit to caching the inverse of a matrix rather than compute it repeatedly (there are also alternatives to matrix inversion that we will not discuss here). Your assignment is to write a pair of functions that cache the inverse of a matrix.

Write the following functions:

- makeCacheMatrix: This function creates a special “matrix” object that can cache its inverse.
- 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 the cacheSolve should retrieve the inverse from the cache.

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.

For this assignment, assume that the matrix supplied is always invertible.

## My Solution

cachematrix.R:

## Testing My Functions

Another useful example.

A good example for getting a better understanding of differences amount formal parameters, local variables and free variables . And it is also helpful for learning the <<- operator.

open.account.R:

Simple tests for open.account function:

More information about Scope .

- 1. Introduction
- 2. Example: Caching the Mean of a Vector
- 3. Assignment: Caching the Inverse of a Matrix
- 4.1. Functions
- 4.2. Testing My Functions
- 5. Another Useful Example

## R-Programming_Week3 Programming Assignment 2_ Lexical Scoping 01292016

R-Programming_Week3_Programming Assignment 2: Lexical Scoping 01292016

## The repository for this project is empty

- Copy SSH clone URL [email protected]:mzheng3/r-programming_week3-programming-assignment-2_-lexical-scoping-01292016.git
- Copy HTTPS clone URL https://git.unl.edu/mzheng3/r-programming_week3-programming-assignment-2_-lexical-scoping-01292016.git
- Clone with SSH
- Clone with HTTPS
- Open in your IDE Visual Studio Code (SSH) Visual Studio Code (HTTPS) IntelliJ IDEA (SSH) IntelliJ IDEA (HTTPS)

## IMAGES

## VIDEO

## COMMENTS

A Medicare identification number, also known as an HIC Number, is a unique identification code assigned to each beneficiary on his Medicare card. This number is used both for identification purposes and to confirm or deny eligibility for ce...

ESPN3 does not have an assigned channel on DirecTV. ESPN3 is an online-only broadcast service that streams live events and replays of ESPN programming. DirecTV customers who subscribe to ESPN can access ESPN3 through the WatchESPN website a...

The reading level of a book is one way parents and teacher can gauge whether a child can read a particular book independently. There are several ways to calculate reading levels. These programs take into account factors like vocabulary and ...

Peer-graded Assignment: Programming Assignment 2: Lexical Scoping -- for Douglas Rubin, BMS - Week 3 programming assignment_b.r.

Here is my submission of the programming assignment 2 in R for Caching the Inverse of a Matrix.

Assignment: Caching the Inverse of a Matrix. Matrix inversion is usually a costly computation and there may be some benefit to caching the

Introduction This second programming assignment will require you to write an R function that is able to cache potentially time-consuming

In this Programming Assignment will take advantage of the scoping rules of the R language and how they can be manipulated to preserve state

Functions ... ## benefit to caching the inverse of a matrix rather than compute it repeatedly. ... ## stores a matrix and caches its inverse. ##

So it has to have 2 variable x the matrix, and inv the inverse. Each variable has 2 functions set and get . So, set is a function that takes an

Rstudio en español |Coursera en español | Programming Assignment 2: Lexical ScopingAnterior| 2020. 1K views · 3 years ago ...more. PAL

This video contains the code for programming assignment-3 and the step by step instructions for submission of assignment which includes

Week 2 Highlights: Lexical scoping as the reason why all objects must be stored in memory. Programming assignment is useful. As seen below, I have decided to do

R-Programming_Week3_Programming Assignment 2: Lexical Scoping 01292016.