简体   繁体   English

Octave中的函数句柄格式

[英]Function handle formats in Octave

A function handles in Octave is defined as the example below. Octave中的函数句柄定义如下。

f = @sin;

From now on, calling function f(x) has the same effect as calling sin(x) . 从现在开始,调用函数f(x)与调用sin(x)具有相同的效果。 So far so good. 到现在为止还挺好。 My problem starts with the function below from one of my programming assignments. 我的问题从下面的一个编程任务开始。

function sim = gaussianKernel(x1, x2, sigma)

The line above represents the header of the function gaussianKernel . 上面的行代表函数gaussianKernel的标题。 This takes three variables as input. 这需要三个变量作为输入。 However, the call below messes up my mind because it only passes two variables and then three while referring to gaussianKernel . 但是,下面的调用让我大吃一惊,因为它只引用了两个变量,然后是三个引用gaussianKernel

model = svmTrain(X, y, C, @(x1, x2) gaussianKernel(x1, x2, sigma));

Shouldn't that be simply model = svmTrain(X, y, C, @gaussianKernel(x1, x2, sigma)); 不应该只是model = svmTrain(X, y, C, @gaussianKernel(x1, x2, sigma)); ? What is the difference? 有什么不同?

You didn't provide the surrounding code, but my guess is that the variable sigma is defined in the code before calling model = svmTrain(X, y, C, @(x1, x2) gaussianKernel(x1, x2, sigma)); 你没有提供周围的代码,但我的猜测是在调用model = svmTrain(X, y, C, @(x1, x2) gaussianKernel(x1, x2, sigma));之前,代码中定义了变量sigma model = svmTrain(X, y, C, @(x1, x2) gaussianKernel(x1, x2, sigma)); . It is an example of a parametrized anonymous function that captures the values of variables in the current scope. 它是参数化匿名函数的一个示例,它捕获当前作用域中变量的值。 This is also known as a closure . 这也称为封闭 It looks like Matlab has better documentation for this very useful programming pattern. 对于这个非常有用的编程模式,看起来Matlab有更好的文档

The function handle @gaussianKernel(x1, x2, sigma) would be equivalent to @gaussianKernel . 函数句柄@gaussianKernel(x1, x2, sigma)等同于@gaussianKernel Using model = svmTrain(X, y, C, @gaussianKernel(x1, x2, sigma)); 使用model = svmTrain(X, y, C, @gaussianKernel(x1, x2, sigma)); might not work in this case if the fourth argument of svmTrain is required to be a function with two input arguments. 如果svmTrain的第四个参数需要是一个带有两个输入参数的函数,则在这种情况下可能不起作用。

The sigma variable is already defined somewhere else in the code. sigma变量已在代码中的其他位置定义。 Therefore, svmTrain pulls that value out of the existing scope. 因此, svmTrain将该值从现有范围中拉出。

The purpose of creating the anonymous function @(x1, x2) gaussianKernel(x1, x2, sigma) is to make a function that takes in two arguments instead of three. 创建匿名函数@(x1, x2) gaussianKernel(x1, x2, sigma)是创建一个接受两个参数而不是三个参数的函数。 If you look at the code in svmTrain , it takes in a parameter kernelFunction and only calls it with two arguments. 如果你看一下svmTrain中的代码,它会接受一个参数kernelFunction并且只用两个参数调用它。 svmTrain itself is not concerned with the value of sigma and in fact only knows that the kernelFunction it is passed should only have two arguments. svmTrain本身并不关心sigma的值,事实上只知道它传递的kernelFunction应该只有两个参数。

An alternate approach would have been to define a new function: 另一种方法是定义一个新函数:

function sim = gKwithoutSigma(x1, x2)
    sim = gaussianKernel(x1, x2, sigma)
endfunction

Note that this would have to be defined somewhere within the script calling svmTrain in the first place. 请注意,这必须首先在调用svmTrain的脚本中的某处定义。 Then, you could call svmTrain as: 然后,您可以将svmTrain称为:

model = svmTrain(X, y, C, @gKwithoutSigma(x1, x2))

Using the anonymous parametrized function prevents you from having to write the extra code for gKwithoutSigma . 使用匿名参数化函数可以防止您必须为gKwithoutSigma编写额外的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM