one vs all regression

You can actually substitute.

for i=1 : num_labels

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

all_theta(i,:)=theta;

try this

for i = 1:num_labels,
    [all_theta(i,:)] = fmincg (@(t)(lrCostFunction(t, X, (y == i), lambda)), initial_theta, options);
end;

you also don't need to initialize all_theta in the beginning


fmincg takes the handle of the objective function as the first argument, which in this case is a handle to lrCostFunction.

If you go inside fmincg.m, you will find the following lines:

argstr = ['feval(f, X'];                      % compose string used to call function

%---Code will not enter the following loop---%
for i = 1:(nargin - 3) %this will go from 1 to 0, thus the loop is skipped
   argstr = [argstr, ',P', int2str(i)];
end
% following will be executed
argstr = [argstr, ')'];

At the end of above code snippet, result will be,

argstr=feval(f,X');

If you a little ahead, you will see,

[f1 df1] = eval(argstr);                      % get function value and gradient

Therefore, the function handle f will run with an argument X'. Therefore, t=X', which makes sense too. The initial theta will converge to give you the final parameter vector for logistic regression.