Array indices must be positive integers or logical values.
1 次查看(过去 30 天)
显示 更早的评论
clearvars
% Setup
t_max= 1200; % in s
k=0.01; % in s^-1
g_nr=2; % in molecules/s
t_nr=0; % in s
x_nr=5; % in molecules
time_log=0;
% Simulation of the non-regulated construct
t=0;
prod_rate=g_nr;
deg_rate=k*x_nr;
while t<t_max
wait_time=-log(rand)/(prod_rate+(deg_rate*x_nr(t_nr)));
prob_prod=prod_rate/(prod_rate+deg_rate*x_nr(t_nr)); % Propensity of production
t=t+wait_time; % Update current time
t_nr=t_nr+1; % Update the number of steps (reactions) associated with the experiment.
time_log(t_nr)=t; % Add the current time to the time log
if rand < prob_prod % Defines whether production takes place based on Monte Carlo method.
x_nr(t_nr)=x_nr(t_nr-1)+1; % Implements production
else
x_nr(t_nr)=x_nr(t_nr-1)-1; % Implements degradation
end
end
if t>t_max
t_nr(end)=[];
x_nr(end)=[];
end
close all
plot(time_log,t_nr,'r','LineWidth',2)
hold on
plot(time_log,x_nr,'r','LineWidth',2)
I get the error Array indices must be positive integers or logical values in line 24:
wait_time=-log(rand)/(prod_rate+(deg_rate*x_nr(t_nr)));
Does anyone know why this is?
7 个评论
Image Analyst
2022-6-6
And what about the help you got below, you know, in the official Answers section of this page? Did you see it? If not, scroll down.
回答(2 个)
Image Analyst
2022-6-5
Since this is one of the most common FAQs, see the FAQ for a thorough discussion:
0 个评论
Torsten
2022-6-6
if rand < prob_prod % Defines whether production takes place based on Monte Carlo method.
x_nr(t_nr)=x_nr(t_nr-1)+1; % Implements production
else
x_nr(t_nr)=x_nr(t_nr-1)-1; % Implements degradation
end
The first time you enter this if-clause, t_nr = 1.
Thus you address x_nr(t_nr-1) = x_nr(0).
This throws an error in MATLAB since array indices start with index 1, not 0.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!