Error using vertcat Dimensions of arrays being concatenated are not consistent.

1 view (last 30 days)
Dear all,
I implement this 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,:) ;
SP1=(sum(exp(h1*-dt)))/m ;
for i=1:k-1
h=(hzrd(i,:)+hzrd(i+1,:)).*-dt;
SP_others(i)=sum(exp(h),2)./m ;
SP_Simulation=[SP1 ; SP_others];
end
end
when I try to use it for the following main code:
k=5;
m=3000;
b=0.01 ;
SP=repmat(0.98,k,1) ;
w=rand(k,m);
fun=@(a) (get_survivalprobability(a,b,w,dt,m,k)-SP);
% find f(a)=0 such that equation (4.6) holds, given
% an initial guess a0
a0=ones(1,k);
[a] = fsolve(fun,a0);
% find hazard rates with the new parameters a
for i=1:k
for j=1:m
h(i,j)=exp(a(i)+b*w(i,j))
end
end
it shows me the following error: Error using vertcat Error using vertcat Dimensions of arrays being concatenated are not consistent.
Could someone please help me??
Thank you in advance
Regards,
Martina

Answers (1)

darova
darova on 11 Sep 2019
MATLAB doesn't know if SP_others is column or row vector. By default - row
SP_others(i)=sum(exp(h),2)./m ;
Then you are trying to concantenate
SP_Simulation=[SP1 ; SP_others];
Also i recommend you to pre-allocate memory for matrices
111Untitled.png
hzrd = zeros(k,m);
SP_others = zeros(k-1,1); % COLUMN

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!