Having trouble with array dimensions in an arrayfun call with fminsearch nested in it
2 次查看(过去 30 天)
显示 更早的评论
Hey all,
I've been working on a multivariable nonlinear regression problem, and ran into some trouble in the regression part of it. The code is as follows:
options = optimset('MaxFunEvals',10,'MaxIter',8);
c0 = zeros(100,1);
c_hat = arrayfun(@(a,b) fminsearch(@(c) 1/2*(a(1,1:2)*a(1,1:2)'-(b*c)*(b*c)')+lambda*sum(c),100,options),y,Phi);
Here are some notes I have about the issue.
- size(y) = size(a) = 2x100
- size(Phi) = size(b) = 2x100
- y is originally 2x1 but has been padded with 0's for the sake of accomodating parameter requirements for arrayfun
- Can't index into a using a(1,1:2) because a has not been defined yet, but I need to pad the a in order to use arrayfun. Catch22
- want size(b*c) = 2x1, and accordingly, size(c) = 1x100. The returned coefficients as a result of minimization, c_hat, would therefore 1x100, as I'd want it to be.
How do I address this? Thanks in advance!
0 个评论
采纳的回答
Walter Roberson
2017-8-6
When you arrayfun() with matrices, the calculation is done with one element from each input matrix, so scalars are going to be passed into the function. You cannot then index the scalars.
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.
It appears to me you are trying to index a column of phi at a time. If so then what you should be passing to arrayfun is the vector of column numbers. Perhaps something like
c0 = ones(1,100);
c_hat = arrayfun(@(colidx) fminsearch(@(c) 1/2*(y(1:2,colidx).'*y(1:2,colidx)-(phi(:,colidx)*c)*(y(1:2,colidx)*c))+lambda*sum(c), c0, options), 1:size(phi,2), 'uniform', 0 );
You should re-check what I have written carefully as I might not have the right row vs column mix in the multiplications; I am pretty sure I got it wrong in places, because I am not certain what your calculation is trying to do.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!