Matlab assumes a matrix is 1x2 instead of 1x9?
Show older comments
I have the following code: b should end up as a 1x9 matrix of values solved for in the equation.
D0=D(1,:); D1=D(2,:); D2=D(3,:); D3=D(4,:); D4=D(5,:); D5=D(6,:); D6=D(7,:); D7=D(8,:); D8=D(9,:);
Data=zeros(15,9);
Input=Data104cis;
b0=[1,.1,.1,.1,.1,.1,.1,.1,.1];
for jj=1:15;
y=Input(jj);
fun = @(b) ((b(1).*D0) +(b(2).*D1)+ (b(3)*D2)+(b(4).*D3)+(b(5).*D4)+ (b(6).*D5) +(b(7).*D6)+(b(8)*D7)+(b(9)*D8));
OLS =@(b) sum(y - fun(b)).^2;
fminsearch(OLS,b0);
Data(jj,:)=b;
b0=b;
end
instead, b somehow ends up as a 1x2 matrix and then the code crashes with a dimension mismatch error. I have no idea why it would assume b to be 1x2 when i have 9 b values shown in fun.
Answers (2)
Star Strider
on 10 Jun 2014
You’re not asking fminsearch to return anything!
Change:
fminsearch(OLS,b0);
to:
b = fminsearch(OLS,b0);
and see if that solves your problem.
Your posted code doesn't define b anywhere, so if it is 1x2, it is because you assigned it that somewhere prior to the code you've shown. Presumably, you meant to assign b to the output of fminsearch,
b=fminsearch(OLS,b0);
Incidentally, fun() could be defined much more simply and efficiently as
fun=@(b) b(:).'*D;
3 Comments
John D'Errico
on 10 Jun 2014
But why would anyone use fminsearch to compute a 9 (NINE!) parameter OLS estimator? I've heard of using a Mack truck to take a pea to Boston, but this is different. Here Darya is using a human powered tricycle to pull a loaded boxcar with a full load of iron ore to Boston.
Time to learn how to use backslash for Darya!
Yes. It is also suspicious that y is a scalar,
y=Input(jj);
The solutions Data(jj,:) should all be identical, differing only by scale factors of Input(jj).
Darya
on 10 Jun 2014
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!