Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the @ symbol used for in Octave?

Tags:

octave

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.

like image 664
Gregory Arenius Avatar asked Sep 27 '18 23:09

Gregory Arenius


2 Answers

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.

like image 198
Tasos Papastylianou Avatar answered Nov 10 '22 13:11

Tasos Papastylianou


@ 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))

like image 37
Clinton Winant Avatar answered Nov 10 '22 15:11

Clinton Winant