GithubHelp home page GithubHelp logo

programmingassignment2's Introduction

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

  1. set the value of the vector
  2. get the value of the vector
  3. set the value of the mean
  4. get the value of the mean
makeVector <- function(x = numeric()) {
        m <- NULL
        set <- function(y) {
                x <<- y
                m <<- NULL
        }
        get <- function() x
        setmean <- function(mean) m <<- mean
        getmean <- function() m
        list(set = set, get = get,
             setmean = setmean,
             getmean = getmean)
}

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.

cachemean <- function(x, ...) {
        m <- x$getmean()
        if(!is.null(m)) {
                message("getting cached data")
                return(m)
        }
        data <- x$get()
        m <- mean(data, ...)
        x$setmean(m)
        m
}

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:

  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.

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.

In order to complete this assignment, you must do the following:

  1. Fork the GitHub repository containing the stub R files at https://github.com/rdpeng/ProgrammingAssignment2 to create a copy under your own account.
  2. Clone your forked GitHub repository to your computer so that you can edit the files locally on your own machine.
  3. Edit the R file contained in the git repository and place your solution in that file (please do not rename the file).
  4. Commit your completed R file into YOUR git repository and push your git branch to the GitHub repository under your account.
  5. Submit to Coursera the URL to your GitHub repository that contains the completed R code for the assignment.

Grading

This assignment will be graded via peer assessment.

programmingassignment2's People

Contributors

gustavdelius avatar rdpeng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

programmingassignment2's Issues

BriannaAssignment2

makeCacheMatrix <- function(x = mat()) {
a<-NULL
set<-function(y){
x<<-y
a<<-NULL
}
get<-function() x
setmat<-function(solve) a<<- solve
getmat<-function() a
list(set=set, get=get,
setmat=setmat,
getmat=getmat)
}
cacheSolve <- function(x=mat(), ...) {
a<-x$getmat()
if(!is.null(a)){
message("getting cached data")
return(a)
}
mat<-x$get
a<-solve(mat, ...)
x$setmat(a)
a
}

Assignment2

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

cacheSolve <- function(x, ...){
inv <- x$getInv()
if(!is.null(inv)){
message('getting cached data')
return(inv)
}
matrix <- x$get()
inv <- mean(matrix,...)
x$setInv(inv)
inv
}

Sync Error

Hi,

I am new to Github, When I am trying to sync the committed changes I am getting an error stating "Sync Failed to push local changes - It seems you do not have permission to push your changes to this repository". Can anyone please help me with this, as today is the last day to complete and submit the assignment

Thanks in advance..

cacheSolve

cacheSolve <- function(x, ...) {
inv <- x$getinverse()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data, ...)
x$setinverse(inv)
inv
}

Assignment2

makeVector<-function(x=numeric()){

  • m<-NULL
  • set<-function(y){
  • x<<-y
  • m<<-NULL
  • }
  • get<-function()x
  • setmean<-function(mean)m<<-mean
  • getmean<-function()m
  • list(set=set,
  • get=get,
  • setmean=setmean,
  • getmean=getmean)
  • }

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

  • m<-x$getmean()
  • if(!is.null(m)){
  • message("getting cached data")
  • return(m)
  • }
  • data<-x$get()
  • m<-mean(data,...)
  • x$setmean(m)
  • m
  • }

Programming Assignment 2

makeCacheMatrix <- function(x = matrix()) {
## set the matrix
inv <-NULL
setMatrx<-function(mat){
m<<-mat
}
## get the matrix
getMatrix<-function() m
## set the inverse
cacheInverse<-function(temp){
inv<<-temp
}
## get the inverse
getInverse<-function(){
if (nrow(m) != ncol(m)) {print('matrix is not square')}
inv
}

list(setMatrix=setMatrix,getMatrix=getMatrix,cacheInverse=cacheInverse,getInverse=getInverse)

}

cacheSolve <- function(x) {
n<-s$getInverse()
if (!is.null(n)){
## get it from the cache and skips the computation.
message("getting cached data")
return(n)
}
## sets the value of the inverse in the cache via the setinv function.
n<-solve(s$getMatrix())
s$cacheInverse(n)
n
}
mywork.R.zip

ProgramingAssignment2

makeVector <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setsolve <- function(solve) m <<- solve
getsolve <- function() m
list(set = set, get = get,
setsolve = setsolve,
getsolve = getsolve)
}

cachesolve <- function(x, ...) {
m <- x$getsolve()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data, ...)
x$setsolve(m)
m
}

Programming Assignement 2

These are the 2 functions

Make Cache Matrix:

makeCacheMatrix <- function(x = matrix()){
    inv <- NULL
    set <- function(y){
        x<<- y
        inv<<- NULL
    }
    get <- function() x
    setInv <- function(solve) inv<<- solve
    getInv <- function() inv
    list(set = set, get = get,
         setInv = setInv,
         getInv = getInv)

Solve the matrix, and check the result (the product of the Source Matrix and its inverse must be 1).

cacheSolve <- function(x, ...){
    inv <- x$getInv()
    if(!is.null(inv)){
        message('getting cached data')
        return(inv)
    }
    sourcematrix <- x$get()
    inv <- solve(sourcematrix,...)
    x$setInv(inv)
    inv
    sourcematrix %*% inv
}

Programming Assignment 2 #

Sorry this is the wrong link!!!
The right link is https://github.com/bxjaj/Programming-Assignment-2-/tree/master
Please go the that link to check my assignment!
I can't change the link once I accidentally upload the wrong one, really sorry for that.

sapplymakeCacheMatrix <- function(x = matrix()) {
## set the matrix
## get the matrix
## set the inverse
## get the inverse
## this list is used as the input to cacheSolve()
inv_x <- NULL
set <- function(y) {
## use "<<-" to assign a value to an object in an environment
x <<- y
inv_x <<- NULL
}
get <- function() x
setinse<- function(inse) inv_x <<-inse
getinse <- function() inv_x
list(set = set, get = get,
setinse = setinse,
getinse = getinse)
}

cacheSolve <- function(x, ...) {
inv_x <- x$getinse()
if (!is.null(inv_x)) {
## get it from the cache and skips the computation.
message("getting cached inverse matrix")
return(inv_x)
} else {
inv_x <- solve(x$get())
## sets the value of the inverse in the cache via the setinv function.
x$setinse(inv_x)
return(inv_x)
}
}

assignment2 luyangyi

makeMatrix<-function(x=numeric()){
m<-NULL
set<-function(y){
x<<-y
m<<-NULL
}
get<-function() x
setinverse<-function(inverse) m<<-inverse
getinverse<-function() m
list(set=set,get=get,setinverse=setinverse,getinverse=getinverse)
}
cashemean<-function(x,...){
m<-x$getinverse()
if(!is.null(m)){
message("getting cashed data")
return(m)
}
data<-x$get()
m<-solve(data,...)
x$setinverse(m)
m
}

A2 - Jiaxing Kuang

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
}

Assignment2

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

cacheSolve <- function(x, ...){
inv <- x$getInv()
if(!is.null(inv)){
message('getting cached data')
return(inv)
}
matrix <- x$get()
inv <- mean(matrix,...)
x$setInv(inv)
inv
}

ProgrammingAssignment2

makeCasheMatrix <- function(x=matrix()){
m <- matrix()
set <- function(y){
x <<- y
m <<- matrix()
}
get <- function() x
setinv<- function(inv) m <- inv
getinv <- function() m
list(set=set, get=get,
setinv=setinv,
getinv=getinv)
}

cacheSolve <- function(x,...){
m <- x$getinv()
if(!is.na(m)){
message("getting cache data")
return(m)
}
data <- x$get()
m <- solve(data)
x$setinv(m)
m
}

ProgrammingAssignment2

Programming Assignment 2

in the first part create the function which can cache its inverse

contains set and get to assign a value to matrix

set and get the value of inverted matrix

makeCacheMatrix <- function(x = matrix()) {
inverted <- NULL
set <- function(y) {
x <<- y
inverted <<- NULL
}
get <- function() x
setInverted <- function(inverse) Inverted <<- inverse
getInverted <- function() Inverted

    list(set = set, get = get,
         setInverted = setInverted,
         getInverted = getInverted)

}

calculate inverted matrix

cacheSolve <- function(x, ...) {
m <- x$getInverted()
if(!is.null(m)) {
message("getting cached data")
return(Inverted)
}
m <- x$get()
Inverted <- solve(m, ...)
x$setInverted(Inverted)
Inverted
}

Programming Assignment 2

makeCacheMatrix <- function(x = matrix()){
inver <- NULL
set <- function(y) {
x <<- y
inver <<- NULL
}
get <- function() x
setInver <- function(inver) inver <<- inver
getInver <- function()inver
list(set = set,
get = get,
setInver = setInver,
getInver = getInver)
}

cacheSolve <- function(x,...) {
inver <- x$getInver()
if(!is.null(inver)) {
message("getting cached data")
return(inver)
}
matri <- x$get()
inver <- solve(matri,...)
x$setInver(inver)
inver
}

The cacheSolve template has a confusing varargs (ellipsis / ...) argument

The cacheSolve function is intended to return the inverse of a matrix. A clear, simple, objective. However, it's template has variable arguments (...) that are not further specified. That's confusing, because what is the intention? While it surely is possible to resolve this issue, IMHO it distracts from the core assignment - getting familiar with git(hub) and lexical scoping.

The standard approach (for newbies, at least) to deal with the arguments is to pass them to solve(). And cache the response as the inverse. While this is fine when no additional arguments are present on any of the invocations, imagine a vector argument b being there on the first invocation of cacheSolve. Not the inverse of the matrix m will be cached, but the solution x to m %*% x = b.

This might be related to the unfortunate name 'cacheSolve'. Such a name implies a more efficient implementation of solve when the same matrix is used repeatedly. But that's different from just getting the inverse. For the former definition, an additional argument b is to be expected - that's in the contract that solve() offers. The latter definition is a more specialized "solve" that only calculates the inverse, but doesn't offer the capability of solving a set of linear equations.

So IMO there are two ways to resolve the issue. One is to drop the ... argument and rename the function to cacheInverse (and include the hint to solve() in the comments). The other is to (only) support the b argument, with a default value that implies that the inverse should be returned.

Programming Assignment 2

makeCacheMatrix <- function(x = matrix()) {
m<-NULL
set<-function(y){
x<<-y
m<<-NULL
}
get<-function() x
setmatrix<-function(solve) m<<- solve
getmatrix<-function() m
list(set=set, get=get,
setmatrix=setmatrix,
getmatrix=getmatrix)
}

cacheSolve <- function(x=matrix(), ...) {
m<-x$getmatrix()
if(!is.null(m)){
message("getting cached data")
return(m)
}
matrix<-x$get
m<-solve(matrix, ...)
x$setmatrix(m)
m
}

Programming Assignement 2

###############Create a matrix

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

########Solve the inverse of the matrix

cacheSolve <- function(x, ...) {
matinv <- x$getinv()
if(!is.null(matinv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data, ...)
x$setinv(inv)
inv
}

Example is flawed: additional parameters are not checked.

The example implementation is (deeply?) flawed: cachemean allows passing through additional parameters to mean, but successive calls may have different parameters. Since the values are not checked for, the mean cached after the first call is returned even though it's (arguably) wrong:

> x <- makeVector(c(1:100,200))
> cachemean(x)
[1] 51.9802
> cachemean(x, trim=0.1)
getting cached data
[1] 51.9802
> mean(x$get(), trim=0.1)
[1] 51

Similar examples can be constructed with the na.rm parameter of mean.

Proposed solution: Drop the ... parameter from cachedmean.

(That is, if it is possible to check the parameters and/or cache one value per parameter combination, that may be a good solution as well. But 1) that's probably beyond the course at this point and 2) it may be a recipe for memory leaks.)

I cannot see my committed file, and the URL.

Please help. I made two commits from my local repository to the master. ( commit, because, push is not working ). I have 2 SHA1 keys. Where can I see these and how can this be considered as submitted ?????

ProgrammingAssignment2

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

    set <- function(y){
            x <<- y
            matrix_inv <<- NULL
    }
    get <- function() x
    setinverse <- function( inverse) matrix_inv <<- inverse
    getinverse <- function() matrix_inv



    list(set = set, get = set, 
         setinverse = setinverse, getinverse = getinverse)

}

cacheSolve <- function(x, ...){
matrix_inv <- x$getinverse()
if(!is.null(matrix_inv)){
message("getting cached data")
return(matrix_inv)
}

    my_matrix <- x$get()
    matrix_inv <- solve(my_matrix, ...)
    x$setinverse(matrix_inv)

    inv

}

I'm not understanding GitHub functionality.

I'm about to quit on this.
I've got the "cachematrix.R" code in the ProgrammingAssignment2 repository in the windows gitHub on my pc, and I think I've loaded it up on my online kd6888 gitHub account, but I have no idea what the last statement in the exercise means:

Submit to Coursera the URL to your GitHub repository that contains the completed R code for the assignment.

I can see in my online GitHub kd6888 repository that my hash code is 82d5b3c
but I've got no idea what to do after that to "submit" it to Coursera.

R-Programming_ProgrammingAssignment2

'makeCacheMatrix' creates a special "matrix" object while 'cacheSolve' function computes the inverse of the special "matrix" returned.

'makeCacheMatrix' creates a special "matrix" object that can cache its inverse.

makeCacheMatrix <- function(x = matrix()) {
int <- NULL
set <- function(a) {
dig <<- a
int <<- NULL
}
get <- function() dig
setinverse <- function(inverse) int <<- inverse
getinverse <- function() int
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}

'cacheSolve' function computes the inverse of the special "matrix" returned by makeCacheMatrix above and

if the inverse has already been calculated (and the matrix has not changed),

then cacheSolve retrieve the inverse from the cache.

cacheSolve <- function(dig, ...) {
int <- int$getinverse() ## Return a matrix that is the inverse of 'dig'
if(!is.null(int)) {
message("getting cached data")
return(int)
}
else{
data <- dig$get()
int <- mean(data, ...)
dig$setinverse(int)
int
}
}
cachematrix.txt

Programming Assignment 2

Put comments here that give an overall description of what your

functions do

Write a short comment describing this function

makeCacheMatrix <- function(x = matrix()) {
i <- NULL
set <- function(y){
x <<- y
i <<- NULL
}
get <- function() x
setInv <- function(inv){
i <<- inv
}
getInv <- function() i
list(set = set, get = get,
setInv = setInv,
getInv = getInv)

}

Write a short comment describing this function

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

Assignment is underspecified

The assignment is worded as follows:

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.

Lest we assume that we should just copy-paste the example (which is, of course, essentially what this assignment is about, isn't it?) there are two things missing (comparing with the example):

  1. Should the special matrix be mutable, i.e. do we have to have a set() method as in the example?
  2. How should cacheSolve change the given special matrix? In particular, should it store the value it computes? What should it do if the given object is not of the required form?

makeCacheMatrix

makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setinverse <- function(solve) inv <<- solve
getinverse <- function() inv
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}

makeCacheMatrix

makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setinverse <- function(solve) inv <<- solve
getinverse <- function() inv
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}

A2 - Jiaxing Kuang

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)
}

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
}

ProgrammingAssignment 2

Put comments here that give an overall description of what your

functions do

Write a short comment describing this function

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

Write a short comment describing this function

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

ProgrammingAssignment2_Inverse Matrix

makeCachMatrix creates a special "matrix"

object that can cache its inverse:

1. set the value of the matrix

2. get the value of the matrix

3. set the value of the solve (inverse)

4. get the value of the solve (inverse)

we are assuming that the matrix supplied is always invertible

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

set <- function(y)
{
x <<- y
m_inv <<- NULL
}

get <- function() x
setm_inv <- function(solve) m_inv <<- solve

getm_inv <- function() m_inv

list(set = set, get = get,
setm_inv = setm_inv,
getm_inv = getm_inv)
}

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.

cacheSolve <- function(x, ...) {
m_inv <- x$getm_inv()
if(!is.null(m_inv)) {
message("getting cached data")
return(m_inv)
}
data <- x$get()
m_inv <- solve(data, ...)
x$setm_inv(m_inv)
m_inv
}

Proposal for an alternative to achieve caching

I realize that API styles are a matter of taste. FWIW, I think this is a nicer way of achieving caching functionality since it's harde to misuse:

makeVector <- function(x = numeric()) {
  m <- NULL

  set <- function(y) {
    x <<- y
    m <<- NULL
  }

  get <- function() x

  mean <- function() {
    if ( is.null(m) ) {
      # Note that mean(x) would not work -- then the function would
      # call itself!
      m <<- base::mean(x)
    }
    m
  }

  list(set = set, get = get, mean = mean)
}

versioned here

File Upload Disabled/Blocked

I am trying to upload my assignment 2 (cached matrix code) and I can not figure out what I am doing wrong. GitHub is not very user friendly!

unable to fork the programmingAssignment2

I am unable to fork the programmingAssignement2 repository to my account. A popup window appears asking "where to fork this repsitory". I chose my account name, but nothing happens. Please help me in solving this issue..

rrrainbowww

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)
}

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
}

ProgrammingAssignment2/cachematrix.R

makeCacheMatrix <- function(x = matrix())
{
inv=NULL
set=function(y)
{
x<<-y
inv<<-NULL
}
get=function(){return(x)}
setInverse=function(i){inv<<-i}
getInverse=function(){return(inv)}
output=list(set=set,get=get,setInverse=setInverse,getInverse=getInverse)
return(output)
}
cacheSolve <- function(x, ...)
{
inv=x$getInverse()
if(!is.null(inv))
{
message("getting cached data\n")
return(inv)
}
data=x$get()
inv=solve(data,...)
x$setInverse(inv)
return(inv)
}

quanwaidian R programing assignment2

makeCacheMatrix <- function(x = matrix()) {
m<-NULL
set<-function(y){
x<<-y
m<<-NULL
}
get<-function() x
setmatrix<-function(solve) m<<- solve
getmatrix<-function() m
list(set=set, get=get,
setmatrix=setmatrix,
getmatrix=getmatrix)
}

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

Cache inverse matrix

https://github.com/spguerrerot/ProgrammingAssignment2

Author: Sandra Guerrero

Return the inverse of matrix with determinant that is not zero, and save the result in cache

This function creates a special "matrix" object that can cache its inverse.

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

This function computes the inverse of the special "matrix" returned by makeCacheMatrix,

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(m, ...) {
inverse <- m$getInverse()
if(!is.null(inverse)) {
message("getting cached data")
return(inverse)
}
data <- m$get()
inverse <- solve(data, ...)
m$setInverse(inverse)
inverse
}

Example

matriz <- matrix(c(3:10, 10), 3, 3)

cacheSolve(makeCacheMatrix(matriz))

output

[,1] [,2] [,3]

[1,] -3.333333 4 -1

[2,] 3.333333 -5 2

[3,] -1.000000 2 -1

makeCacheMatrix

makeCacheMatrix<-function(x=matrix()){
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get<-function()x
setsolve<-function(solve) m<<-solve
getsolve<-function() m
list(set=set,get=get,
setsolve=setsolve,
getsolve=getsolve)
}

cachesolve <- function(x) {
m <- x$getsolve()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data)
x$setsolve(m)
m
}

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.