Help with code logic
1 回表示 (過去 30 日間)
古いコメントを表示
Hello everyone,
I was hoping to get some help with my code. I am creating a text file with a certain required format, but I am having problems listing the frequencies when the steps are greater than 3. Let me elaborate a bit more... I have a lower and upper frequency and a change of frequency steps (delta). I want to list the number of frequencies (integer) and then all the frequencies (integers) from the lower to the upper frequency (i.e. if lower is 2 and upper is 8 with delta of 3, my number of frequencies will be 3 and my frequencies will be 2, 5, 8).
The problem that I am having is that if the steps are greater than 3, when I find "numfreq" it gives me an extra frequency because of the rounding (i.e. lower 2 and upper 13 with delta of 4, gives me 4 frequencies instead of 3 with the last frequency past the upper frequency; the frequencies are 2, 6, 10, 14). I tried many things like checking for even or odd, mod equal or not equal to zero, decreasing the for loop, etc; but every time I fix something, something else goes wrong.
I know I am in the right track, but at this point I am out of ideas. I hope my question is clear enough, but any help will be greatly appreciated it.
Here is the part of the code in question:
% Works for steps of 1-3
lower = 2;
upper = 13;
steps = 4;
if mod(upper-lower,steps) ~= 0
upper = upper - 1;
end
numFreq = round(((upper-lower)/steps)+1)
allFreqs = [1:numFreq];
allFreqs(1) = lower;
for i = 2:numFreq
allFreqs(i) = allFreqs(i-1) + steps;
end
% check to see what all frequencies are
allFreqs
Thanks
4 件のコメント
Matt Kindig
2013 年 9 月 9 日
In that case, wouldn't the colon operator do what you want?
allFreqs = lower:steps:upper
Or is there something else that I'm not seeing?
回答 (1 件)
Image Analyst
2013 年 9 月 9 日
Why not use linspace()???
allFreqs = linspace(lower, upper, steps);
I think that's how most MATLABers would do it.
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!