what does this command: "@(x) Function" means in matlab?
215 views (last 30 days)
Show older comments
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 Comment
James Carter
on 18 Jul 2019
Edited: James Carter
on 18 Jul 2019
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
Accepted Answer
James Tursa
on 13 Sep 2016
Edited: James Tursa
on 13 Sep 2016
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 Comments
More Answers (0)
See Also
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!