"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

KSSV
KSSV 2017년 9월 21일
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?
selin
selin 2017년 9월 21일
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
selin
selin 2017년 9월 21일
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