@(t) mean in Matlab? [duplicate]

As the title suggest, what exactly does @(t) mean in Matlab given the context below? computeNumericalGradient is a function and cofiCostFunc is also a function that takes in a bunch of parameters. The question is what exactly is the @(t) doing to the cofiCostFunc function?

computeNumericalGradient( ...
                @(t) cofiCostFunc(t, Y, R, num_users, num_movies, ...
                                num_features, lambda), [X(:); Theta(:)]);

Solution 1:

@(t) is what is known as an anonymous function. @(t) will thus return a handle to a function that takes in one variable t. Basically, it's a function that takes in one parameter, t. The rest of the parameters are defined previously in your workspace.

What you are doing here is that the first parameter to computeNumericalGradient takes in a function where t is a variable that is defined by you. As such, your computeNumericalGradient takes in two parameters:

  1. An anonymous function that is defined like before.
  2. A single 1D vector with two column vectors concatenated with each other - The first column is X, and the second column is Theta.

As a sidenote, if you were to do this:

func = @(t) cofiCostFunc(t, Y, R, num_users, num_movies, num_features, lambda);

You would thus call this function by doing func(t), where t is whatever variable you want that is relevant to the function at hand. The code would thus simplify to:

computeNumericalGradient(func, [X(:); Theta(:)]);

I'm not familiar with what you're doing here, so that context will have to be figured out by you.

Solution 2:

It is a handle to an anonymous function. I'm guessing computeNumericalGradient is expecting a function with one parameter (t). This essentially redefines cofiCostFunc as a function of only t by providing the remaining values.

You can read more here:

http://www.mathworks.com/help/matlab/matlab_prog/symbol-reference.html#bsgigzp-4