在MATLAB中使用@()调用函数 [英] Call functions with @() in MATLAB
问题描述
我试图在以下代码片段中找出@(t)的目的:
I'm trying to figure out what is the purpose of @(t) in the following code snippet:
[theta] = ...
fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), ...
initial_theta, options);
lrCostFunction:
lrCostFunction:
function [J, grad] = lrCostFunction(theta, X, y, lambda)
%LRCOSTFUNCTION Compute cost and gradient for logistic regression with
%regularization
% J = LRCOSTFUNCTION(theta, X, y, lambda) computes the cost of using
% theta as the parameter for regularized logistic regression and the
% gradient of the cost w.r.t. to the parameters.
和选项:
options = optimset('GradObj', 'on', 'MaxIter', 50);
我希望能得到一些解释.预先感谢
I'd appreciate some explanation. Thanks in advance
推荐答案
Let me answer your question focusing on anonymous function itself.
以下功能,在单独的.m文件中定义
The following function, defined in a separate .m file
function y = foo(x, a, b)
y = x^(a-b);
end
等同于在主脚本中定义匿名函数
is equivalent to defining an anonymous function in the main script
bar = @(x, a, b) x^(a-b);
当主脚本调用函数foo(5, 1, 2)
时,Matlab在工作目录中搜索,然后读取并执行文件foo.m
中的代码.相反,当您运行bar(5, 1, 2)
行时,Matlab会调用函数句柄" 并将其视为函数(尽管其功能受一行代码限制-您无法轻松执行switch
或for
之类的操作).
When your main script calls function foo(5, 1, 2)
, Matlab searches in working directory, then reads and executes code within file foo.m
. Contrarily, when you run a line bar(5, 1, 2)
, Matlab calls a "function handle" and treat it as a function (though its power is limited by a single line of code - you can't perform things like switch
or for
easily).
有时,我们需要将一些功能包装到易于使用的功能中.考虑一种情况,我们希望对foo
进行1000次评估,但只有输入x
会发生变化,而a
和b
保持不变.当然可以在for
循环中编写foo(x, 1, 2)
,但是您也可以在进入循环之前包装该函数.
Sometimes we need to wrap some function into an easier-to-use one. Consider a case where we want to evaluate foo
1000 times, but only input x
changes, while a
and b
remains same. It's of course OK to write foo(x, 1, 2)
in the for
loop, but you can also wrap the function before going into the loop.
a = 1;
b = 2;
foobar = @(x) foo(x, a, b);
当调用foobar(5)
时,Matlab首先调用函数句柄foobar
,将5
作为其唯一输入.该函数句柄有一条指令:调用名为foo
的另一个函数(或函数句柄(如果已定义)). foo
的参数是:x
,当用户调用foobar(x)
时定义.在执行功能句柄定义代码之前首先定义的a
和b
.
When you call foobar(5)
, Matlab first invokes the function handle foobar
, taking 5
as its only input. That function handle has one instruction: call another function (or function handle, if you define it as so) named foo
. The arguments of foo
are: x
, which is defined when user calls foobar(x)
; a
and b
, which have been defined in the first place BEFORE the function handle definition code is executed.
在您的情况下,fmincg
仅接受仅具有一个输入参数的函数作为其第一个参数.但是lrCostFunction
需要四个. fmincg
不知道如何治疗x
,y
或lambda
(我也不是).因此,将成本函数包装为一般优化器可以理解的形式是您的工作.这还需要您预先分配x
,y
,c
和lambda
.
In your case, fmincg
only accepts, as its first argument, a function that only has one input argument. But lrCostFunction
takes four. fmincg
doesn't know how to treat x
, y
, or lambda
(I don't either). So it's your job to wrap the cost function into the form that a general optimizer can understand. That also requires you assign x
, y
, c
and lambda
in advance.
这篇关于在MATLAB中使用@()调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!