Help with code logic
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Matt Kindig
el 9 de Sept. de 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?
Respuestas (1)
Image Analyst
el 9 de Sept. de 2013
Why not use linspace()???
allFreqs = linspace(lower, upper, steps);
I think that's how most MATLABers would do it.
1 comentario
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!