Hi I tried to run the following and it keeps giving me the following. Can somebody help me fix it.

1 次查看(过去 30 天)
close all clear all
%Question # 1D
alpha=1 beta=2 sigma=2 BetaOLS_Vector=ones(1000,2) for i=1:1000 x=normrnd(2,1,[200,1]); e=normrnd(0,sigma,[200,1]); y=alpha + beta*x +e(i); Con=ones(200,1); xn=[Con x]; BetaOLS_Vector(i)= inv(xn'*xn)*xn'*y end
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ProblemTwoQuestionOne (line 16) BetaOLS_Vector(i)= inv(xn'*xn)*xn'*y

采纳的回答

Roger Stafford
Roger Stafford 2014-9-19
The problem here is that inv(xn'*xn)*xn'*y has two elements in a size 2 x 1 vector, while you are trying to place them in a single element position in BetaOLS_Vector(i). Matlab doesn't like that.
Probably you mean this:
BetaOLS_Vector(i,:)= inv(xn'*xn)*xn'*y;

更多回答(1 个)

Image Analyst
Image Analyst 2014-9-19
BetaOLS_Vector(i must be a single number. Evidently inv(xn'*xn)*xn'*y is not a single number. Check the dimensions of xn and y and make sure it works out to a scalar.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by