What is the @ symbol used for in Octave?
For example, in the code:
[theta, cost] = fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);
I have a general understanding of what the code is doing but I don't get what the @(t)
is there for. I've looked around the octave documentation but the @
symbol seems to be a hard term to search for.
From the console:
octave:1> help @
-- @
Return handle to a function.
Example:
f = @plus;
f (2, 2)
=> 4
(Note: @ also finds use in creating classes. See manual chapter
titled Object Oriented Programming for detailed description.)
See also: function, functions, func2str, str2func.
More info in the manual: https://octave.org/doc/interpreter/Function-Handles.html
In your specific code, the '@' syntax is used to create an "on-the-spot" implementation of a function (in the form of an anonymous function), that takes a single argument, as opposed to the three required by your costFunction
one. This is because fminunc expects a function that takes a single argument to work, and therefore one effectively 'wraps' the more complex function into a simpler one compatible with fminunc.
@ precedes the dummy variable in the definition of anonymous functions, for instance:
f = @(x) x.^2;
y=[1:3];
f(y)
returns
1 4 9
a quick look at help fminunc shows that FCN in your example is @(t)(costFunction(t, X, y))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With