R 编程:缓存矩阵的逆 [英] R programming: cache the inverse of a matrix

查看:20
本文介绍了R 编程:缓存矩阵的逆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Coursera 上的数据科学课程,我对其中一项作业有疑问,在该作业中我必须对矩阵求逆,然后缓存该结果.

I am following a Data Science course on Coursera and I have a question regarding one of the assignments where I have to inverse a Matrix and then cache that result.

基本上我一直在谷歌上搜索并找到了答案,但我还没有理解部分答案.出于这个原因,我还不想提交我的作业,因为我不想提交任何我不完全理解的东西.

Basically I have been googling away and I found the answer but there are parts of the answer that I do not yet understand. For this reason I don't want to submit my assignment yet since I don't want to submit anything that I do not fully understand.

从下面的代码中我不明白的部分是定义setInverse的部分.函数(逆)inv"从何而来?尤其是逆"从未定义过?

The part that I do not understand from the code below is the part where setInverse is defined. where does the 'function(inverse) inv' come from? especially the 'inverse' was never defined?

在此之后返回一个列表,这对我来说也没有多大意义?

After this a list is returned which does not make much sense to me as well?

如果有人能花时间向我解释这个功能,我将不胜感激!

If someone could take the time to explain this function to me I would be very grateful!

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


## Write a short comment describing this function

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
}

推荐答案

我不知道你的确切任务,但我会稍微改变你的功能:

I don't know your exact assignment, but I would change your function slightly:

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

然后您可以像这样使用它:

You can then use it like this:

funs <- makeCacheMatrix()
funs$set(matrix(1:4, 2))
funs$get()
#     [,1] [,2]
#[1,]    1    3
#[2,]    2    4
funs$setInverse()
funs$getInverse()
#     [,1] [,2]
#[1,]   -2  1.5
#[2,]    1 -0.5

该练习可能旨在教您闭包.重点是xinv存储在set,get,的封闭环境中setInversegetInverse 函数.这意味着定义它们的环境,即由 makeCacheMatrix() 调用创建的环境.看到这个:

The exercise is probably intended to teach you closures. The point is that x and inv are stored in the enclosing environment of the set, get, setInverse, getInverse functions. That means the environment within which they were defined, i.e., the environment created by the makeCacheMatrix() call. See this:

ls(environment(funs$set))
#[1] "get"        "getInverse" "inv"        "set"        "setInverse" "x"

如您所见,此环境中不仅有四个函数,还有 xinv 对象(使用 <<-).而 getgetInverse 函数只从它们的封闭环境中获取这些.

As you see not only are the four functions in this environment, but also the x and inv objects (a consequence of using <<-). And the get and getInverse functions only fetch these from their enclosing environment.

这篇关于R 编程:缓存矩阵的逆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆