How to fix my my convolution function?
古いコメントを表示
Hi, my teacher wants us to make a convolution function instead of using matlabs and this is what i have
x=[1 2 3 4] y=[1 2 3 4]
function P = MyConv1(x,y)
p = length(x); q = length(y);
for n = 1:p+q-1
for k = 1:n
P(n) = [sum(x(k)*y(n-k))];
end
end
however i get this error:
Attempted to access y(0); index must be a positive integer or logical.
Error in MyConv1 (line 11) P(n) = [sum(x(k)*y(n-k))];
someone please help me figure out what the issue is
回答 (1 件)
Image Analyst
2014 年 3 月 19 日
0 投票
Then add 1. n-k is 0 and MATLAB doesn't allow a zero index. Try n-k+1.
2 件のコメント
Andrew
2014 年 3 月 19 日
Image Analyst
2014 年 3 月 19 日
Just write the arrays down on paper. Slide them across and see what gets multiplied. It's not that hard to see what to do if you do that. If you really can't figure it out, come back.
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!