what is the error with my code

1 回表示 (過去 30 日間)
Mostafa Anwar
Mostafa Anwar 2021 年 6 月 3 日
編集済み: Adam Danz 2021 年 6 月 8 日
N = 4;
x = [ 3 4 5 6 ];
for k = 0 : N-1
for n=0:N-1
sum = sum + x.*(exp(-1*1i*2*pi*k.*n/N));
end
end
stem(sum)

回答 (2 件)

Adam Danz
Adam Danz 2021 年 6 月 3 日
編集済み: Adam Danz 2021 年 6 月 8 日
> what is the error with my code
The error is,
N = 4;
x = [ 3 4 5 6 ];
for k = 0 : N-1
for n=0:N-1
sum = sum + x.*(exp(-1*1i*2*pi*k.*n/N));
% ^^^ ERROR
end
end
Error using sum
Not enough input arguments.
stem(sum)
What is causing the error (the question you intended to ask)?
sum() is a very commonly used Matlab function.
You're using it as a variable name but Matlab doesn't know it's a variable name because you haven't declared it as a variable by assiging a value to it.
Solution
Don't use sum as a variable name.

Mahaveer Singh
Mahaveer Singh 2021 年 6 月 3 日
% give the initial value of sum.
% like sum=0 (initial vlue of sum)
N = 4;
x = [ 3 4 5 6 ];
sum=0;
for k = 0 : N-1
for n=0:N-1
sum = sum + x.*(exp(-1*1i*2*pi*k.*n/N));
end
end
stem(y)
  1 件のコメント
Adam Danz
Adam Danz 2021 年 6 月 3 日
編集済み: Adam Danz 2021 年 6 月 3 日
Even if this is within its own function workspace, I advise against using sum as a variable name.
  1. It's a very commonly used Matlab function.
  2. It makes the code more difficult to read because when people see sum they expected it to be the function.

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

カテゴリ

Help Center および File ExchangeGet Started with DSP System Toolbox についてさらに検索

タグ

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by