0
$\begingroup$

I am following Andrew Ng's Coursera class on machine learning, and I came across this syntax for the fminunc and fmincg functions:

fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)),initial_theta, options);

Specifically, it is the @ that's confusing me. I read up on it and I still don't understand what we're doing with it here. Can someone clarify what this is doing, as well as how the t in the function call to lrCostFunction is related to the @(t) in the beginning.

$\endgroup$
0
$\begingroup$

@(something) is used to call a function in MATLAB or octave.

Suppose you create a function within a code. And you set a keyword for that function. we have a sum(x,y) function which takes two inputs and returns the sum. Now you fix the value of y, say y = 3; And you want to change the value of x every time. You can design the inline function by following:

y=3
temp = @(p) sum(p,y);

ret = temp(4);  % What this will do is it will add 4 with 3 and return the answer.

This is used where we have to call a function multiple times and we don't want to write the arguments every time. It's a shortcut method.

In your case

fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)),initial_theta, options);

the value of t will be replaced every time with initial_theta. That is how the fmincg or fminunc functions work.

$\endgroup$
0
$\begingroup$

The answer from Sabbir helped but I was still confused. After more reading I learned that the "@(something)" identifies the input variable to the private function being created. In other words, in his example above:

temp = @(p) sum(p,y);

the temp function is expecting a value for p to be input when calling temp.

temp(4)

The value y is supplied by a globally available variable that you've already created. I actually couldn't get his example to run but when I used:

temp = @(p) y+p;

it worked as expected:

y = 4
temp(3)

ans = 7
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.