I have Loop from 0.1:0.1:0.1 and I want to skip some cases in this loop like 0.3, 0.7 0.9

 Accepted Answer

Try this:
v = 0.1:0.1:1;
exclude = [0.3, 0.7, 0.9];
[L1,L2] = ismembertol(v, exclude, 0.01);
new_v = v(~L1)

4 Comments

@Najiya Omar: It is essential to use ismembertol, not ismember or setdiff, because comparisons of floating point numbers are influenced by rounding:
any((0:0.1:1) == 0.3) % FALSE!!!
This happens because 0.1 + 0.1 + 0.1 does not equal 0.3 exactly.
Star Strider's solution considers this. +1
@Jan — Thank you!
Thank you both It works
@Najiya Omar —
If my Answer helped you solve your problem, please Accept it!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!