How do I modify the following for loop with an if statement so that it only displays even values of m
for m=1:100
num = 1/(m+1)
end

답변 (2개)

Purushottama Rao
Purushottama Rao 2015년 9월 23일

2 개 추천

If you want to run entire loop on even numbers
you can try
for m=2:2:1000
.....
.....
end
If your loop runs on the entire range of [1 1000], and you want to display only even numbers then you can use
for m=1:1000
.....
if (mod(m,2)==0)
....
end
.....
end
Jon
Jon 2015년 9월 23일

0 개 추천

Use the rem command. If the remainder is zero after dividing the number by 2, display it. I'm purposely not writing the code.

카테고리

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

제품

질문:

2015년 9월 23일

답변:

2015년 9월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by