- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习 Coursera 上的数据科学类(class),我对其中一项作业有疑问,在该作业中我必须对矩阵求逆,然后缓存该结果。
基本上我一直在谷歌上搜索并找到了答案,但我还没有理解部分答案。出于这个原因,我不想提交我的作业,因为我不想提交任何我不完全理解的东西。
从下面的代码中我不明白的部分是 setInverse 定义的部分。 “函数(逆)inv”从何而来?尤其是“逆”从未定义过?
在此之后返回一个列表,这对我来说也没有多大意义?
如果有人能花时间向我解释这个功能,我将不胜感激!
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
}
最佳答案
我不知道你的确切任务,但我会稍微改变你的功能:
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)
}
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
存储在
set
的封闭环境中,
get
,
setInverse
,
getInverse
职能。这意味着定义它们的环境,即由
makeCacheMatrix()
创建的环境。称呼。看到这个:
ls(environment(funs$set))
#[1] "get" "getInverse" "inv" "set" "setInverse" "x"
x
和
inv
对象(使用
<<-
的结果)。和
get
和
getInverse
函数只从它们的封闭环境中获取这些。
关于R 编程 : cache the inverse of a matrix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33738820/
问题来自 Julia Discourse 我正在使用 Julia 1.2。这是我的测试: a = rand(1000, 1000) b = adjoint(a) c = copy(b) @btime
给定两个 m x n 矩阵 A 和 B,其元素属于集合 S。 问题:A 的行和列可以置换为 B 吗? 解决这个问题的算法的复杂度是多少? 行列式有部分帮助(当 m=n 时):必要条件是 det(A)
这是共同计算平移和旋转的正确方法,还是有更好的方法?目前我的代码先翻译然后旋转,这会造成问题吗? 代码 from math import cos, sin, radians def trig(angl
如何在 core.matrix 中将矩阵和向量元素相乘? 我正在尝试找到与以下 Octave 音程代码等效的 core.matrix: A = [1 2 3 4; 5 6 7 8] B = [2; 3
我是 Scilab 的新用户(另见 here)。 我定义了一个简单的分段函数,并在使用该函数 ( "Warning adding a matrix with the empty matrix will
我有一个像这样的 RDD: (A,AA,1) (A,BB,0) (A,CC,0) (B,AA,2) (B,BB,1) (B,CC,4) 我想将其转换为以下 RRD: ([1,0,0],[2,1,4])
我使用的矩阵如下 (require '[clojure.core.matrix :as ccm]) (def M (ccm/matrix [[1 2] [3 4]])) (ccm/mset! M 0
我有一个矩阵类,它有一组函数,其中一个是矩阵运算符++(); 构造函数: Matrix(int num_rows,int num_col,int initialization,double initi
我有一个矩阵如下; 1 2 3 4 5 1 0 1 1 0 0 2 0 0 1 1 0 3 1 0 0 0 1 4 0 0 1 0 0
我已经部署了为家庭服务器(synapse)运行的单个实例,并附加了多个域作为 example.com 和 example1.com。我想创建像 [email protected] 这样的用户和 [em
我有 200 个向量;每一个的长度都是 10000。 我想填充一个矩阵,使每一行代表一个向量。 最佳答案 如果你的向量已经存储在一个数组中,那么你可以在这里使用 vcat( ): A = [rand(
如何向现有矩阵添加行或列?我正在尝试添加一个偏差项(一列)作为矩阵的第一行。在 Octave 中我可以这样做: M = [ones(size(M, 1), 1), M]; 最佳答案 您可以使用 joi
我正在使用 GNU GSL 进行一些矩阵计算。我正在尝试将矩阵 B 与矩阵 A 的逆矩阵相乘。 现在我注意到 GSL 的 BLAS 部分有一个函数可以做到这一点,但前提是 A 是三角形。这有什么具体原
我想计算如下:Matrix * Matrix Matrix有大约 6M*3 个元素,如何转换 Matrix至 Matrix这样我就能得到 Matrix结果。 最佳答案 您可以使用 Map 函数将 do
我只是没有看到我的错误。关于此错误消息的问题太多了,答案要么不适用,要么我只是看不到它们适用。也许应该改进错误消息? Matrix a = Matrix(3, 4); // fill a with v
在android.opengl.Matrix类中有两种旋转矩阵的方法,它们是: static void rotateM (float[] m, int mOffset, float a, float
我正在使用 C++ 进行编码,并且使用的是 FEniCS fenics/2016.1.0。我的部分代码是 Matrix A; Vector f; std::vector> dirichlet_matr
JAMA(用于矩阵计算的 java 库)中的 JAMA:Matrix.times() 与 Matrix.arrayTimes() 有什么区别 如果我有一个d维度 vector x和一个k维度 vect
我试图做的是简单地将 cublasDgemm(矩阵-矩阵乘法)应用于多个具有“双”(8 字节)类型元素的矩阵,所有这些元素都具有一个非常大的维度。在我的例子中,矩阵的大小是 12755046 x 46
我正在尝试使用 android Matrix 对象旋转给定的位图。 我想将它发送到我的服务器,我正在使用 Android API8。 我应该使用 Matrix.setRotate 还是 Matrix.
我是一名优秀的程序员,十分优秀!