Good night. I have a homework of Coursera. But i have two days stuck trying to solve my problem.
My homework is:
Write the following functions:
makeCach
Given the code in the OP, x$getinverse()
should return NULL
because one must execute cacheSolve()
in order to populate the cache. I explain the details of how the sample code for this assignment works, including the need for the second function to populate the cache, in the stackoverflow answer Caching the Mean of a Vector.
That said, the program has three defects that prevent it from operating correctly.
cacheSolve()
, m<-x$getinverse
sets the value of m
to a function, not the result of executing the getinverse()
function cacheSolve()
, data<-x$get
returns the address of the function get()
instead of its contents. cacheSolve()
, x$setinverse(m)
fails because the function setinverse()
in makeCacheMatrix
does not include an input argument. Note that since I am a Community Mentor for the Hopkins R Programming course, I'm not allowed to post a complete solution because it would violate the Coursera Honor Code.
Once the errors are corrected, the code works like this:
> x <-makeCacheMatrix(matrix(c(1,0,0,0,1,0,0,0,2),ncol=3,nrow=3))
> cacheSolve(x)
[,1] [,2] [,3]
[1,] 1 0 0.0
[2,] 0 1 0.0
[3,] 0 0 0.5
> x$get()
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 2
> x$getinverse()
[,1] [,2] [,3]
[1,] 1 0 0.0
[2,] 0 1 0.0
[3,] 0 0 0.5
>