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

211 Ansichten (letzte 30 Tage)
Saeed
Saeed am 13 Sep. 2016
Bearbeitet: mohd El Tahir am 29 Mär. 2021
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 Kommentar
James Carter
James Carter am 18 Jul. 2019
Bearbeitet: James Carter am 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

James Tursa
James Tursa am 13 Sep. 2016
Bearbeitet: James Tursa am 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 Kommentare
Kong
Kong am 26 Feb. 2020
maybe Matlab
mohd El Tahir
mohd El Tahir am 29 Mär. 2021
Bearbeitet: mohd El Tahir am 29 Mär. 2021
matlab and also works in octave

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by