Index Exceeds Matrix Dimensions

When I run the code below I get the error "index exceeds matrix dimensions' I'm trying to plot time on the x-axis and s,i,r on three plots. Thank you
pops=[s;i;r];
for n=1:run_count
for j=1:n
% loop to build up matrix of population values
lambda=mu*(totpop)+alpha*i;
news=s+lambda-beta*s*i-mu*s;
newi=i+beta*s*i-(alpha+k)*i-mu*i+p*r;
newr=r+k*i-mu*r-p*r;
s=news;
i=newi;
r=newr;
beta=normrnd(2.3e-9,2.3e-9*.1);
k=normrnd(1.417e-6,1.417e-6*.1);
p=normrnd(.0024,.0024*.1);
pops=[pops,[s;i;r]];
end
% plot result
subplot(3, 1, 1);
plot ([0:n],pops(1,:));
xlabel ('time');
ylabel ('susceptible');
hold on;
subplot(3, 1, 2);
plot ([0:n], pops(2,:));
xlabel ('time');
ylabel ('infected');
hold on;
subplot(3, 1, 3);
plot ([0:n],pops(3,:));
xlabel ('time');
ylabel ('recovered');
hold on;
if true
% code
end
end

回答 (1 件)

James Tursa
James Tursa 2017 年 6 月 15 日

0 投票

You can use the debugger for this. First, type in the following at the command line:
dbstop if error
Then run your code. When the error is encountered, the code will pause at the offending line with all current variables intact. Then you can examine the size of the variables that are causing the problem. Backtrack in your code to figure out why the sizes and/or indexes are not what you expected at that line.

この質問は閉じられています。

タグ

質問済み:

2017 年 6 月 15 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by