# Octave: fmincg and Anonymous Functions

Yao Yao on July 26, 2014

One-vs-all Classfication 的作业里用到了 fmincg，但是语法有点奇怪：

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


function [J, grad] = lrCostFunction(theta, X, y, lambda)


forum 里 The meaning of @(t) in fmincg ? 解释得非常好：

The @-symbol means that what we are sending to the fmincg-function, lrCostFunction, is not a matrix, vector or number, but a function. And the (t) symbol means that it is a function of t. lrCostFunction is a function of four varibles, but with the command @(t)lrCostFunction(t,X,(y==c),lambda) it becomas a function of one varible, t, and the rest of the varablas are set to X, (y==c), and lambda.

We use this notation because fmincg minimize a function with respect to one varible.

public ReturnType anony(Theta t) {
return lrCostFunction(t, X, (y == c), lambda);
}


blog comments powered by Disqus