Why am I getting "Array indices must be positive integers or logical values" error?

Array indices must be positive integers or logical values.
Error in A2Q1>LinearConv (line 68)
yc(i) = yc(i) + x1(i-j+1).*x2(j);
function yfnc = LinearConv(x1, x2, len)
yc = zeros(1, len);
for i = 1:len
%Total terms will be 1 to len
for j = 1:(i+1)
if(i-j+1<length(x1) && j<length(x2))
yc(i) = yc(i) + x1(i-j+1).*x2(j);
%Equation
end
end
end
yfnc = yc;
end

1 件のコメント

Shubh Shah
Shubh Shah 2021 年 1 月 22 日
Thanks for correcting me, I forgot to change j's iteration from 1 to i

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

 採用された回答

Stephen23
Stephen23 2021 年 1 月 22 日
編集済み: Stephen23 2021 年 1 月 22 日
Consider this indexing:
x1(i-j+1)
What value does it have on the last iteration of the j loop? (hint: zero).
i-j+1 % that index
j = i+1 % last iteration
i-(i+1)+1 % substitute j
i-i-1+1 % zero

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Types についてさらに検索

質問済み:

2021 年 1 月 22 日

コメント済み:

2021 年 1 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by