Info

# arrayfun() mistakes a vector optimization parameter for a scalar optimization parameter

1 view (last 30 days)
Alex Huang on 7 Aug 2017
Commented: Stephen23 on 7 Aug 2017
Hey guys, I have a problem with the function arrayfun not recognizing the dimensionality of the variable I'm trying to extract from it. The code is here:
options = optimset('MaxFunEvals',10,'MaxIter',10);
*c_hat = [c_hat; arrayfun(@(idx) fminsearch(@(c) 1/2*sumsqr(SST(idx,1)-Phi(1,idx)*(c.*rC(idx,1))) + lambda*sum(c.*rC(idx,1)),1,options),1:size(SST,2))];* %broke up the line into two
%size(SST) = 413x1
%size(Phi) = 413x413
%size(rC) = 413x1
%size(c_hat) = 413x1
Background: This is a minimization problem, I'm using multivariable nonlinear regression to find the value of 413 coefficients, to be contained in the vector c. However, the way MATLAB interprets it, c is a scalar. This isn't surprising because looking closely at the code, fact that c is a scalar is just as plausible as the fact that c is vector of size 413 (in both cases, the minimization function line
c_hat = [c_hat; arrayfun(@(idx)
fminsearch(@(c) 1/2*sumsqr(SST(idx,1)
-Phi(1,idx)*(c.*rC(idx,1)))
+ lambda*sum(c.*rC(idx,1)),1,options)
,1:size(SST,2))];
is valid and will run). That being said, how do I let MATLAB know that c is not a scalar, but a vector?
Stephen23 on 7 Aug 2017

Jiro Doke on 7 Aug 2017
Isnt't it because you are passing in "1" (scalar) as the initial guess to fminsearch? Perhaps you can pass in a 1x413 vector of ones:
c_hat = [c_hat; arrayfun(@(idx) fminsearch(@(c) 1/2*sumsqr(SST(idx,1)- ...
Phi(1,idx)*(c.*rC(idx,1))) + lambda*sum(c.*rC(idx,1)),ones(1,413),options), ...
1:size(SST,2))];
Walter Roberson on 7 Aug 2017
Right. As I posted in your other Question,
"If you want c to be a vector of length 100, then you need to pass a vector of length 100 in the position where you have passed 100. The first parameter to fminsearch is a function handle; the second parameter is the starting point vector, not the length of the starting point vector."
Jiro Doke on 7 Aug 2017