Matlab: picking only integers out of a for loop

In summary: In order to use the floor and ceil functions, you need to tell Matlab what representation you're using for the floating point values. For example, in C, you would use the following:float f; f = floor(x); f = ceil(x);
  • #1
parislad
19
0
Hi, I'm new to this forum and only have a few months experience with MATLAB but am getting to know it. Hope you can help me.

I have a for loop which looks like:

for f = a:a:b
command
end

Now,
I only want to execute this command for the integer values in this loop.
eg. if it was f=0.2:0.2:6.2
I only want to run the code for the integer values f=1,2,3,4,5, and 6.
The problem is a and b are values input by the user, so I'm unsure how to do it.

Any help will be appreciated.
Thanks in advance.
Ben
 
Physics news on Phys.org
  • #2
Use the floor and ceil functions.
 
  • #3
jhae2.718's recommendation is good, but it might require a bit of tweaking. Looking at your example, you want "command" to execute when f is 1, 2, 3, 4, 5, and 6. Unfortunately, adding floating point values doesn't work exactly as expected, since the decimal numbers aren't stored in the form you might expect. For example, after the loop runs 5 times f will probably not be exactly 1.0, and I would be willing to bet money that after running 30 times, f will be something other than 6.0.

The reason for this is that certain decimal fractions, especially multiples of 0.2, don't have exact representation as floating point numbers, which are stored as base-16 values. Just as 1/3 doesn't have a finite-length representation as a decimal (i.e., base-10) fraction, fractions such as 1/10 and 1/5 and their multiples don't have finite-length representations as binary floating point numbers. They are stored in a relatively small number of bytes in Matlab (8 bytes, I believe), so the actual value is a little under or a little over the true value. When you add these approximate values repeatedly, the error accumulates.

One way to account for this is to recognize that if the difference of the value of f and the value of floor(f) is small enough, then f is an integer or very close. For "small enough" you could probably use 0.000001.
 
  • #4
Based on mark44's post, I would consider something like this:
Code:
epsilon = 1e-6; [COLOR="SeaGreen"]%or something small[/COLOR]
[COLOR="Blue"]for[/COLOR] i=a:a:b;
    [COLOR="Blue"]if [/COLOR]abs(floor(i)-i) < epsilon;
        [COLOR="SeaGreen"]% commands[/COLOR]
[COLOR="Blue"]    end
end[/COLOR]
 
  • #5
I should add that what I described is not specific to Matlab - it's present in any programming languages that work with floating point numbers that are stored in binary form.
 

Related to Matlab: picking only integers out of a for loop

1. What is a for loop in Matlab?

A for loop in Matlab is a programming construct that allows a certain block of code to be executed repeatedly for a specified number of times. It is often used to perform operations on a set of data or to iterate through a list of elements.

2. How can I only pick integers out of a for loop in Matlab?

In order to only pick integers out of a for loop in Matlab, you can use the "mod" function to check if the current iteration of the loop is an integer. If the remainder of the current iteration divided by 1 is equal to 0, it is an integer and can be selected.

3. Can I use a for loop to iterate through a list of numbers in Matlab?

Yes, a for loop can be used to iterate through a list of numbers in Matlab. You can use the "length" function to determine the size of the list and then use a for loop to iterate through each element.

4. How can I skip non-integer values in a for loop in Matlab?

To skip non-integer values in a for loop in Matlab, you can use the "continue" statement. This will skip the current iteration of the loop and move on to the next one without executing any code within the loop.

5. Is there a more efficient way to pick only integers out of a for loop in Matlab?

Yes, there is a more efficient way to pick only integers out of a for loop in Matlab. Instead of using the "mod" function, you can use the "fix" or "floor" function to round down the current iteration to the nearest integer. This will automatically select only integer values without the need for an if statement.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
29
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
Replies
4
Views
404
  • Engineering and Comp Sci Homework Help
Replies
3
Views
867
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
Back
Top