フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Why do I keep getting the error message array indices must be positive integers or logical values?

1 回表示 (過去 30 日間)
Thomas Clontz
Thomas Clontz 2019 年 9 月 27 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Array indices must be positive integers or logical values.
Error in Drift (line 12)
X(h) = X(0) + mu*h + sigma*Z;
for the following code...
T = 10;
N = 100;
h = T/N;
mu = 0.1;
sigma = 0.2;
X = zeros(1,N+1);
for k = 1:100
for i = 1:N+1
Z = normrnd(0,h);
X(h) = X(0) + mu*h + sigma*Z;
end
plot(Time,X);
hold on
end
  1 件のコメント
John D'Errico
John D'Errico 2019 年 9 月 27 日
MATLAB has an index origin of 1, not 0. So X(0) will fail, and give you exactly that error.

回答 (3 件)

Matt J
Matt J 2019 年 9 月 27 日
編集済み: Matt J 2019 年 9 月 27 日
Looks like this line was accidentally using C-indexing syntax.
X(i) = X(1) + mu*h*(i-1) + sigma*Z;

John D'Errico
John D'Errico 2019 年 9 月 27 日
MATLAB has an index origin of 1, not 0. So X(0) will fail, and give you exactly that error. (Matt pointed that out.)
But also
T = 10;
N = 100;
h = T/N;
...
X = zeros(1,N+1);
X(h) = X(0) + mu*h + sigma*Z;
What is h? h=10/100 = 0.1.
X is a vector. Can you index a vector at the 0.1 location? Where is that? Sort of between location 0 (which does not even exist in MATLAB anyway) and memory location 1?
Sorry. Start with the getting started tutorials. You need to learn how MATLAB does indexing.

Catalytic
Catalytic 2019 年 9 月 27 日
編集済み: Catalytic 2019 年 9 月 27 日
Why do I keep getting the error message array indices must be positive integers
Because the h in X(h) is non-integer and the 0 in X(0) is non-positive.

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

Community Treasure Hunt

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

Start Hunting!

Translated by