Help with code logic

6 views (last 30 days)
David (degtusmc)
David (degtusmc) on 9 Sep 2013
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 Comments
Matt Kindig
Matt Kindig on 9 Sep 2013
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?
David (degtusmc)
David (degtusmc) on 9 Sep 2013
Edited: David (degtusmc) on 9 Sep 2013
No. You are absolutely right. I was just making it harder than what it should be. Also, for some reason I thought the colon operator would have done the same thing that 'linspace' does and split it evenly creating doubles.
Thank you so much Matt!
Could you please add the above as an answer so I can accept it?

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 9 Sep 2013
Why not use linspace()???
allFreqs = linspace(lower, upper, steps);
I think that's how most MATLABers would do it.
  1 Comment
David (degtusmc)
David (degtusmc) on 9 Sep 2013
That would only split thew lower and upper in the amount of steps. The steps (amount of change - delta) is by how much the lower frequency changes up to the upper frequency. I tried using:
linspace(lower, upper, numFreq)
The problem is that I need integers for the frequencies and in some cases I would get doubles. To fix this, I use round but in some cases it rounds to the wrong frequency. For example, with a lower of 2, upper of 9, with delta of 3; gives frequencies of 2, 6, 9 instead of 2, 5, 8 which is what I want.
Luckily, the simple solution provided by Matt is what I needed.
Thank you for the input though.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!