this should be easy I have a while loop that is infinite
古いコメントを表示
When I run this it is inifinite and well that's not what i am looking for... i thought that by limiting the x value to 99 which is in the vector i could get it to stop but that's not the case. any ideas i know that it is going to be something simple...
clc, clear all, close all;
x = 1;
while x <= 99
x = 1:2:100
end
採用された回答
その他の回答 (4 件)
Azzi Abdelmalek
2012 年 9 月 13 日
編集済み: Azzi Abdelmalek
2012 年 9 月 13 日
x = 1;
while x <= 99
x=x+2
% do
end
4 件のコメント
SonOfAFather
2012 年 9 月 13 日
Azzi Abdelmalek
2012 年 9 月 13 日
and what a while loop has to do with that?
SonOfAFather
2012 年 9 月 13 日
Azzi Abdelmalek
2012 年 9 月 13 日
x=1
while x<=100-2
x(end+1)=x(end)+2
end
Wayne King
2012 年 9 月 13 日
編集済み: Wayne King
2012 年 9 月 13 日
Why do you need a while loop here? If you just want a vector running from 1 to 99 in increments of 2, the following does the job.
x = 1:2:100;
With the previous statement you are creating a vector whose last element is 99 and since you don't increment that vector inside the while loop, you never exceed your terminating condition.
note the difference between that and
x = 1;
while x<= 99
x = x+1;
end
x
You see with the above, that when you exit the while loop, x is equal to 100, which terminates the loop. In your example, you never exceed 99 and that is why you have an infinite loop.
Wayne King
2012 年 9 月 13 日
If want you create a vector with a while loop (again not sure why you want to do that), there are many ways to code it.
clear x;
n = 1;
k = 1;
while k<100
x(n) = k;
n = n+1;
k = k+2;
end
3 件のコメント
SonOfAFather
2012 年 9 月 13 日
Azzi Abdelmalek
2012 年 9 月 13 日
Is it a joke?
SonOfAFather
2012 年 9 月 13 日
Andrei Bobrov
2012 年 9 月 13 日
x = 1;
while x(end) < 99
x=[x x(end)+2];
end
カテゴリ
ヘルプ センター および 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!