Index exceeds array bounds error
6 次查看(过去 30 天)
显示 更早的评论
Dear all,
I wrote this simple code:
%input data
SP=[exp(-(0.01*0.5)/(1-0)); exp(-(0.01*1)/(1-0))];
m=3;
k=2;
b=0.01;
dt=0.5;
w=[100 200 300; 100 300 400];
a0=[0 0]; % guess per fsolve
fun=@(a) (get_survivalprobability(a,b,w,dt,m,k)-SP);
[a] = fsolve(fun,a0); % find f(a)=0 ;
for i=1:k % find hazard rates with the new parameters a
for j=1:m
h(i,j)=exp(a(i)+b*w(i,j))
end
end
whose main function that I implemented is:
function [SP_Simulation]=get_survivalprobability(a,b,w,dt,m,k)
for i=1:k
for j=1:m
hzrd(i,j)=exp(a(i)+b*w(i,j)) ;
end
end
h1=hzrd(1,:) ;
SP1=(sum(exp(h1*-dt)))/m ; % find a for the first row vector
for i=1:k-1
h=(hzrd(i,:)+hzrd(i+1,:))*-dt;
SP_others=sum(exp(h),2)/m ;
SP_Simulation=[SP1;SP_others];
end
end
For the previous example, where " w " is a matrix 2x3 , everything works. However, when I want to implement this function for a different input matrix data " w " of dimension 253x100 , the following error displays:
" Index exceeds array bounds. Error in get_survivalprobability (line 6)
hzrd(i,j)=exp(a(i)+b*w(i,j)) ; "
Does anyone know how can I fix it, please?
Thank you in advance.
Regards,
Martina
1 个评论
Adam Danz
2019-7-15
When I use your code but exchange w with a 253x100 matrix, there is no error.
w = randi(500,253,100)
采纳的回答
KALYAN ACHARJYA
2019-7-15
编辑:KALYAN ACHARJYA
2019-7-15
Yes no error with
w=rand(253,100);
Check:
>> whos w
Name Size Bytes Class Attributes
w 253x100 202400 double
3 个评论
KALYAN ACHARJYA
2019-7-15
编辑:KALYAN ACHARJYA
2019-7-15
Second case, I dont expect any error in this case, as you defind
k=2;
m=3;
for i=1:k
for j=1:m
hzrd(i,j)=exp(a(i)+b*w(i,j)) ;
end
....
i iterate from 1 to 2 and j iterate 1 to 3, so the w elements are there with those indices.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!