Clear Filters
Clear Filters


This question is closed. Reopen it to edit or answer.

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

1 view (last 30 days)
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)
+ lambda*sum(c.*rC(idx,1)),1,options)
is valid and will run). That being said, how do I let MATLAB know that c is not a scalar, but a vector?
Thanks in advance!

Answers (1)

Jiro Doke
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), ...
Walter Roberson
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."

This question is closed.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!