Octave 中使用的@ 符号是什么?

Gre*_*ius 5 octave

Octave 中使用的@ 符号是什么?

例如,在代码中:

[theta, cost] = fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);
Run Code Online (Sandbox Code Playgroud)

我对代码在做什么有一个大致的了解,但我不明白@(t)它的用途。我查看了八度音程文档,但该@符号似乎是一个很难搜索的术语。

Tas*_*nou 8

从控制台:

octave:1> help @

 -- @
     Return handle to a function.

     Example:

          f = @plus;
          f (2, 2)
          =>  4

     (Note: @ also finds use in creating classes.  See manual chapter
     titled Object Oriented Programming for detailed description.)

     See also: function, functions, func2str, str2func.
Run Code Online (Sandbox Code Playgroud)

手册中的更多信息:https : //octave.org/doc/interpreter/Function-Handles.html


在您的特定代码中,'@' 语法用于创建函数的“现场”实现(以匿名函数的形式),该实现采用单个参数,而不是您需要的三个参数costFunction一。这是因为 fminunc 需要一个接受单个参数的函数,因此可以有效地将更复杂的函数“包装”成一个与 fminunc 兼容的更简单的函数。


小智 5

@ 在匿名函数的定义中位于虚拟变量之前,例如:

f = @(x) x.^2;
y=[1:3];
f(y)
Run Code Online (Sandbox Code Playgroud)

返回

1 4 9
Run Code Online (Sandbox Code Playgroud)

快速查看 help fminunc 显示您示例中的 FCN 是 @(t)(costFunction(t, X, y))