displaying only even values in a for loop?
67 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
Purushottama Rao
2015-9-23
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
0 个评论
Jon
2015-9-23
Use the rem command. If the remainder is zero after dividing the number by 2, display it. I'm purposely not writing the code.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!