Why I'm getting this error( Array indices must be positive integers or logical values)?

1 回表示 (過去 30 日間)
Sliman Izz
Sliman Izz 2023 年 2 月 9 日
編集済み: Jan 2023 年 2 月 9 日
M=3
W = [0,0,0]
for n = 0:2000
x = cos(0.03 * pi * n)
stem(n,x)
hold on; % hold the plot
end
b= [1 -2 4];
freqz (b,1)
for n = 0:2000
d=conv(x,b)
y=transpose(W)*x
e=d-y;
w(n+1) = w(n)+(2*0.01*e*[x(n),x(n+1),x(n+2)])% µ=0.01;
end
  3 件のコメント
Jan
Jan 2023 年 2 月 9 日
Which lines is failing? Please post the complete error message, which would clarify this detail.
Sliman Izz
Sliman Izz 2023 年 2 月 9 日
Array indices must be positive integers or logical values.
Error in untitled3 (line 16)
w(n+1) = w(n)+(2*0.01*e*[x(0),x(1),x(2)])% µ=0.01;

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

回答 (1 件)

Jan
Jan 2023 年 2 月 9 日
編集済み: Jan 2023 年 2 月 9 日
When I run your code, it stops with the error, the w is undefined in the line:
w(n+1) = w(n)+(2*0.01*e*[x(n),x(n+1),x(n+2)])% µ=0.01;
But even if w exists in the real code, w(0) is not a valid index in the first iteration, because in Matlab indices start at 1.
x(n) will fail also for n=0. But for n=2 there will be an error again, because the first loop creates a scalar x only.
Another bold guess is, that you have redefined a function as an array, e.g. "cos" or "stem".

カテゴリ

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

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by