Why do i receive error message "index exceeds number of array elements" here?

1 vue (au cours des 30 derniers jours)
clear
vthresh = -50;
vreset = -65;
tmax = 2;
vleak = -70;
rmem = 100;
cmem = 0.1;
delta_t = 0.0001;
iappvec = 100:100:600;
tvec = 0:delta_t:tmax;
ref_period = 2.5;
for x = 1:length(iappvec) %cycling through iapp values
vmem=zeros(1,length(tvec));
vmem(1)=vreset;
for i = 2:length(tvec) %forward euler
dqdt=(vleak+vmem(i-1))/rmem+iappvec(x);
dvdt=dqdt/cmem;
vmem(i)=vmem(i-1)+dvdt*delta_t;
spiketime=0;
if vmem>vthresh %spike
vmem=vreset;
spiketime=tvec(i);
end
if tvec(i)<spiketime+ref_period
vmem=vreset;
end
end
end
Index exceeds the number of array elements. Index must not exceed 1.

Réponse acceptée

Image Analyst
Image Analyst le 20 Fév 2022
vmem is a single number, not a vector
vmem(1)=vreset; % vmem = -65
So when you do
dqdt=(vleak+vmem(i-1))/rmem+iappvec(x);
and try to reference vmem(3-1) = vmem(2), well, there is no vmem(2).
Just step through it with the debugger to prove it.
  4 commentaires
Walter Roberson
Walter Roberson le 21 Fév 2022
? Where is vmem a scalar before it is assigned a vector by
vmem=zeros(1,length(tvec));
?
The fix is very likely to change
vmem=vreset;
to
vmem(i)=vreset;
Image Analyst
Image Analyst le 21 Fév 2022
You're right. The first time through it was a vector with 20,001 elements, all zero except for the first one which was -65. The second time through, it was a scalar with a single element/value of -65. The lack of comments makes it hard to figure out what's going on, like when/if you need to reset vmem.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 20 Fév 2022
for x = 1:length(iappvec) %cycling through iapp values
vmem=zeros(1,length(tvec));
vmem is a vector reinitialized for each x value.
if vmem>vthresh %spike
vmem=vreset;
spiketime=tvec(i);
end
if tvec(i)<spiketime+ref_period
vmem=vreset;
end
In both of those conditions, you overwrite all of the vector vmem, making it into a scalar.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by