6

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.

Gregory Arenius
  • 2,904
  • 5
  • 26
  • 47
  • 3
    Possible duplicate of [What is the @ operator in MATLAB?](https://stackoverflow.com/questions/2100595/what-is-the-operator-in-matlab) – Cris Luengo Sep 28 '18 at 00:58
  • 1
    @CrisLuengo I searched for Octave, because that's what I'm using. The answer I got references the Octave manual and the Octave built in help. The question you linked to is about a different program, that I don't use, and the answers to it reference MATLAB documentation, not Octave's. I don't consider this a duplicate. I wouldn't find that answer searching for this one. – Gregory Arenius Sep 28 '18 at 18:39
  • 2
    Duplicate questions are not bad. They are pointers to good answers. You wouldn't find that answer unless this question pointed to it. That is why we flag as duplicate. Sure, MATLAB is a different program, but Octave's mission is to replicate MATLAB's syntax, so reading the MATLAB documentation is a really good way of learning about Octave. MATLAB has excellent documentation. – Cris Luengo Sep 28 '18 at 18:55
  • @CrisLuengo Sure, I've often appreciated duplicate flags pointing to other answers. I just feel that, in this case, even though the answers are similar the question is different. The answer I received is more focused on exactly what I'm using. All good either way. – Gregory Arenius Sep 28 '18 at 18:59
  • 2
    @CrisLuengo One becomes annoyed at this sort of comment: Octave is a separate language from matlab (even though it makes every effort to be the same). Stack Overflow gives it a separate tag. To my knowledge the questions are not cross-listed. Why would someone who has a good question have to look in two places? I wish we spent more time trying to answer questions and less time trying to figure out whether a question is a duplicate or not – Clinton Winant Sep 29 '18 at 01:14

2 Answers2

10

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.

Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
6

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

Clinton Winant
  • 704
  • 10
  • 19