Caching the Inverse of a Matrix

前端 未结 1 1555
渐次进展
渐次进展 2021-01-27 19:55

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

相关标签:
1条回答
  • 2021-01-27 20:19

    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.

    1. In cacheSolve(), m<-x$getinverse sets the value of m to a function, not the result of executing the getinverse() function

    2. In cacheSolve(), data<-x$get returns the address of the function get() instead of its contents.

    3. In 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
    > 
    
    0 讨论(0)
提交回复
热议问题