what does this command: "@(x) Function" means in matlab?
조회 수: 212 (최근 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
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
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 Center 및 File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!