what does this command: "@(x) Function" means in matlab?

215 次查看(过去 30 天)
function mse_calc = mse_test(x, net, inputs, targets)
net = setwb(net, x');
y = net(inputs);
mse_calc = sum((y-targets).^2)/length(y);
end
inputs = (1:10);
targets = cos(inputs.^2);
n = 2;
net = feedforwardnet(n);
net = configure(net, inputs, targets);
h = @(x) mse_test(x, net, inputs, targets);
ga_opts = gaoptimset('TolFun', 1e-8,'display','iter');
[x_ga_opt, err_ga] = ga(h, 3*n+1, ga_opts);
  1 个评论
James Carter
James Carter 2019-7-18
编辑:James Carter 2019-7-18
I am not an expert, but I think I know what's going on here. The @fun (handle AKA pointer) declaration appears to be designed to work on a function of one argument or variable, in the form of fun(x). If you have a routine that takes multiple arguments such as fun(x, y, z), then the pointer construct breaks down as the functions that would call at @fun, such as fzero,have no way of filling in the other arguments.
So the declaration construct of '@(x) fun(x,y,z)' tells Matlab that the variable x is the one to work upon. Note that the y and z need to be defined within the scope of the routine calling this constuct. The example code that you posted shows that 'net' 'inputs' and 'target' are all defined in the scope of the
h = @(x) mse_test(x, net, inputs, targets);
statemet. The variable 'x' is defined in the @(x) declaration syntax.
Anyway this worked for me with
>> test = MScanWvlMap(22500, pMSTarbDnSmth);
>> findFun = @(x) MScanWvlMap(x, pMSTarbDnSmth, test);
>> atest = fzero(findFun,27000)
atest =
2.249999999999971e+04

请先登录,再进行评论。

采纳的回答

James Tursa
James Tursa 2016-9-13
编辑:James Tursa 2016-9-13
That's a function handle. See this link:
The function handle can point to an existing function (e.g., an m-file function), or it can point to an anonymous function (i.e., one created on the fly at the handle creation). E.g.
>> a = 5 % a constant
a =
5
>> b = 3 % another constant
b =
3
>> h = @(x) a*x+b % an anonymous function handle for the linear expression a*x+b
h =
@(x)a*x+b
>> h(4) % evaluate the function handle h for an input of 4
ans =
23
>> h(7) % evaluate the function handle h for an input of 7
ans =
38
  4 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by