TransWikia.com

MATLAB @ symbol for fminunc / fmincg function

Data Science Asked on October 28, 2020

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.

One Answer

@(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.

Answered by Sabbir Ahmed on October 28, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP