Index exceeds array bounds error
Ältere Kommentare anzeigen
Dear all,
I implemented this simple function:
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,:) ; % find a for the first row vector
SP1=(sum(exp(h1*-dt)))/m ;
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
whose main code to test is:
SP_termstruc=repmat(0.98,253,1);
m=3;
k=2;
b=0.01;
dt=0.5;
w=rand(253,100);
a0=[0 0]; % guess per fsolve
fun=@(a) (get_survivalprobability(a,b,w,dt,m,k)-SP_termstruc);
[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
For the previous example, where m=3 (number of columns) and k=2(number of rows) , everything works. However, when I want to test this function for m=100 and k=253 it shows the error " Matrix dimensions must agree. Error in Tesi>@(a)(get_survivalprobability(a,b,w,dt,m,k)-SP_termstruc)
Could someone help me to understand what is wrong, please?
Thank you in advance
Martina
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!