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

 채택된 답변

Star Strider
Star Strider 2018년 1월 23일

2 개 추천

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

Jan
Jan 2018년 1월 23일
@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
Star Strider
Star Strider 2018년 1월 23일
@Jan — Thank you!
Najiya Omar
Najiya Omar 2018년 1월 24일
Thank you both It works
Star Strider
Star Strider 2018년 1월 24일
@Najiya Omar —
If my Answer helped you solve your problem, please Accept it!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2018년 1월 23일

댓글:

2018년 1월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by