How to implement a for loop over a specified range?
51 ビュー (過去 30 日間)
古いコメントを表示
Outputs:
- t: time vector ranging from Start to End with a sampling rate of fs
- X: sum of sinusoids over the specified range
The code I am using for my outputs is:
t = linspace(Start,End,fs);
X=0;
for i=1:N
X = X + A(i)*cos(2*pi*f(i)*t + phi(i)) + B(i);
end
I keep getting an error: Attempted to access B(2); index out of bounds because numel(B)=1. Need help fixing it.
2 件のコメント
Walter Roberson
2016 年 2 月 27 日
We do not know how you initialized B, or what it is intended to mean.
Did you happen to compute B using the "/" operator?
採用された回答
Walter Roberson
2016 年 3 月 1 日
The code you posted would result in A and B being scalars (length 1) if N is empty. If N was not empty, then A and B would only be of length 2 anyhow.
If you are trying to initialize A to be a vector of length N then you would use something like
A = zeros(N, 1);
... but then it would be all zero, which doesn't sound very meaningful.
3 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および 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!