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

1 回表示 (過去 30 日間)
pig master
pig master 2022 年 2 月 20 日
コメント済み: Image Analyst 2022 年 2 月 21 日
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.

採用された回答

Image Analyst
Image Analyst 2022 年 2 月 20 日
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 件のコメント
Walter Roberson
Walter Roberson 2022 年 2 月 21 日
? 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 2022 年 2 月 21 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 2 月 20 日
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.

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by