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

조회 수: 212 (최근 30일)
Saeed
Saeed 2016년 9월 13일
편집: mohd El Tahir 2021년 3월 29일
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
Kong
Kong 2020년 2월 26일
maybe Matlab
mohd El Tahir
mohd El Tahir 2021년 3월 29일
편집: mohd El Tahir 2021년 3월 29일
matlab and also works in octave

댓글을 달려면 로그인하십시오.

추가 답변 (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