Attempted to access dz(1,1000); index out of bounds because numel(dz)=1.

1 回表示 (過去 30 日間)
Alex
Alex 2013 年 10 月 23 日
回答済み: Walter Roberson 2013 年 10 月 23 日
Could you please check my codes? The error message "Attempted to access dz(1,1000); index out of bounds because numel(dz)=1." appears at the third last line.
k = 0.5;
t = 1;
n = 100;
M = 1000;
dt = t/n;
randn('state',101);
dz = randn()*sqrt(dt);
for m = 1:M
for i = 0:n-1
y((i+1),M) = exp(-k*(n*(dt))-i*(dt))*dz((i+1),M);
end;
end;

回答 (2 件)

sixwwwwww
sixwwwwww 2013 年 10 月 23 日
Dear Alex, here is your working code, I made some modifications(If i understood correctly what you want to do):
k = 0.5;
t = 1;
n = 100;
M = 1000;
dt = t/n;
dz = randn(1000)*sqrt(dt);
i = 0:n - 1;
for m = 1:M
y(i+1, m(1:end)) = exp(-k*(n*(dt))-i*(dt))*dz(i+1,m(1:end));
end
In your code you initializing "dz" with 101x101 elemets however in your for loop you were trying to access element out of this range. I hope it is what you need. Good luck!

Walter Roberson
Walter Roberson 2013 年 10 月 23 日
The subexpression dz((i+1),M) on the first trip through the "for i" loop, is going to be trying to use dz((0+1),M) which is dz(1,1000) . But you initialized dz = randn()*sqrt(dt); which is only a single value not a vector of length 1000.
Perhaps you wanted
dz = randn(n, M)*sqrt(dt);
Caution: when you use a variable named "i", then when you refer to "i" in a formula, it becomes difficult to know whether you meant the variable from the for loop, or meant the built-in value for "i" as sqrt(-1). It is recommended that you do not use variables named "i" or "j".

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by