Array indices must be positive integers or logical values.
Afficher commentaires plus anciens
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 commentaires
DGM
le 5 Juin 2022
You're trying to index into x_nr
t_nr=0; % in s
x_nr=5; % in molecules
% ...
x_nr(t_nr)
t_nr is zero. Zero is not a valid array index, as the message states.
x_nr is a scalar. it only has one element, so the only valid value for t_nr is 1
Jonathan Louie
le 5 Juin 2022
"x_nr is supposed to oscillate around 200 ..."
Try changing
deg_rate=k*x_nr; % [1/s]*[molecules] = [molecules/s]
to
deg_rate=k; % [1/s]
The units make more sense that way in this expression:
prod_rate + (deg_rate * x_nr(step_counter))
% molecules/s + ( 1/s * molecules ) = molecules/s <- coherent units
vs:
prod_rate + ( deg_rate * x_nr(step_counter))
% molecules/s + (molecules/s * molecules ) <- incoherent units
And that change also makes x_nr oscillate around 200.
[ Dislcaimer: I have no idea what problem this code is supposed to solve; I'm only observing that setting deg_rate=k seems to have the desired effect (oscillation ~200) and seems to make more sense units-wise than deg_rate=k*x_nr (assuming the units given in the comments at the top are accurate). ]
Jonathan Louie
le 6 Juin 2022
Jonathan Louie
le 6 Juin 2022
Image Analyst
le 6 Juin 2022
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.
Réponses (2)
Image Analyst
le 5 Juin 2022
0 votes
Since this is one of the most common FAQs, see the FAQ for a thorough discussion:
Torsten
le 6 Juin 2022
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.
Catégories
En savoir plus sur Scripts dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!