"index exceeds matrix dimension" problem

Hello,
When I run the code below without an r-loop, I don't have any problems. But when I try to run it like as what is written below, MATLAB is giving "Index exceeds matrix dimension" error. Is there anyone who can help me with this? Thanks in advance!
heye=eye(length(b0),length(b0));
for t=1:length(b0)
heye(t,t)=delt(t);
end
betas1=zeros(length(b0),length(b0));
for r=1:length(b0)
betas1(:,r)=b0;
end
betas1=betas1+heye;
lf1=zeros(numobs,length(b));
%keyboard;
for r=1:length(b0)
b0=betas1(:,r);
A=zeros(numobs,1);
B=zeros(numobs,1);
for i=1:numobs
if c(i)==1
funnon=@(x)exp(-x*b0(1)*t(i))./x;
qnon=integral(funnon,1,1+b0(2)*1);
if qnon~=0
B(i)=qnon;
else
B(i)=0.0000000000000000000000000000000000000000000000001;
end
elseif c(i)==2
funint=@(x)(exp(-x*b0(1)*t1(i))-exp(-x*b0(1)*t2(i)))./x.^2;
qint=integral(funint, 1, 1+b0(2)*1);
if qint~=0
B(i)=qint;
else
B(i)=0.0000000000000000000000000000000000000000000000001;
end
%B(i)=qint;
else
funr=@(x)exp(-x*b0(1)*t(i))./x.^2;
qr=integral(funr, 1, 1+b0(2)*1);
if qr~=0
B(i)=qr;
else
B(i)=0.0000000000000000000000000000000000000000000000001;
end
%B(i);
end
end
for i=1:numobs
if c(i)==1
y(i) = log(b0(1))+log(1+b0(2)*1)-log(b0(2)*1)+log(B(i));
A(i)=y(i);
elseif c(i)==2
y(i)=log(1+b0(2)*1)-log(b0(2)*1)+log(B(i));
A(i)=y(i);
else
y(i)=log(1+b0(2)*1)-log(b0(2)*1)+log(B(i));
A(i)=y(i);
end
end
lf1(:,r)=A;
end

4 个评论

Without specifying the line number of error and having no clue on what are the variables and their dimensions, how you expect us to help you?
I am sorry. b0 is a 2 by 1 vector. numobs is 35781. t, t1, t2, and c are all 35781 by 1 vectors. The error is in funnon=@(x)exp(-x*b0(1)*t(i))./x;
Thanks for your attention!
t is not a 35781 x 1 vector. You have
for t=1:length(b0)
heye(t,t)=delt(t);
end
which is overwriting t with a scalar
That worked! I really appreciate your help, thanks a lot, and have a GOOD day!

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

提问:

2017-9-21

评论:

2017-9-21

Community Treasure Hunt

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

Start Hunting!

Translated by