R编程:缓存矩阵的逆 [英] R programming: cache the inverse of a matrix
问题描述
我正在修读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
该练习可能旨在教您闭包。关键是 x
和 inv
存储在集合的封闭环境中
, get
, setInverse
, getInverse
功能。这意味着定义它们的环境,即 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"
您不仅看到四个函数在这种环境中,还包括 x
和 inv
对象(使用< <-
)。而 get
和 getInverse
函数仅从其封闭环境中获取它们。
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屋!